From patchwork Wed Oct 5 14:52:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Smalley X-Patchwork-Id: 9363079 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 86F686075E for ; Wed, 5 Oct 2016 14:53:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78CB32893D for ; Wed, 5 Oct 2016 14:53:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6D89828A56; Wed, 5 Oct 2016 14:53:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from emsm-gh1-uea11.nsa.gov (emsm-gh1-uea11.nsa.gov [8.44.101.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 388CC2893D for ; Wed, 5 Oct 2016 14:53:57 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.31,449,1473120000"; d="scan'208";a="19824509" IronPort-PHdr: =?us-ascii?q?9a23=3A4fb4FRU3FXy7r/pEywZqYri0vd/V8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZxSEt8tkgFKBZ4jH8fUM07OQ6PG6HzdYqsbY+Fk5M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJP?= =?us-ascii?q?K/jvHcaK1oLshrr0oMWYM1wArQH+SIs6FA+xowTVu5teqqpZAYF19CH0pGBVcf?= =?us-ascii?q?9d32JiKAHbtR/94sCt4MwrqHwI6Loc7coIbYHWN+R9F+QZX3waNDUu6cnqswTT?= =?us-ascii?q?ZRee7XsbFGMNm1xHBBaBpAv7VZf4qCfNvd190SicMNbeR6w1Xyiv9aFmVFnjjy?= =?us-ascii?q?JDfzww62bRotR9lqxaphOmoVp0xIuHTpuSMa9FYq7Ffd4cDVFEV8JVWj0JVpiw?= =?us-ascii?q?dKMTHuEBOqBetIC7qFwQ+0jtTTKwDf/in2cbzkT92rc3hqF7SQw=3D?= X-IPAS-Result: =?us-ascii?q?A2HXAwA8EvVX/wHyM5BcGgEBAQECAQEBAQgBAQEBFgEBAQM?= =?us-ascii?q?BAQEJAQEBgxIBAQEBAR6BU6MKAQEBAQEBBpEGhj4kh3hMAQEBAQEBAQECAQJbJ?= =?us-ascii?q?4IyBAMTBYIYAjcUIAsDAwkCFykICAMBLRUYBwsFGASILbktDAElhXKJOBEBhXo?= =?us-ascii?q?FjjeLQ493Aol9hXICSJApVD8FB4R1VoYWeIEoAQEB?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea11.nsa.gov with ESMTP; 05 Oct 2016 14:51:53 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u95EodAq004336; Wed, 5 Oct 2016 10:50:55 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u95EoZFk165666 for ; Wed, 5 Oct 2016 10:50:35 -0400 Received: from moss-pluto.infosec.tycho.ncsc.mil (moss-pluto [192.168.25.131]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u95EoVxk004328; Wed, 5 Oct 2016 10:50:31 -0400 From: Stephen Smalley To: selinux@tycho.nsa.gov, richard_c_haines@btinternet.com, jason@perfinion.com Subject: [PATCH] libselinux: selinux_restorecon: fix realpath logic Date: Wed, 5 Oct 2016 10:52:41 -0400 Message-Id: <1475679161-24493-1-git-send-email-sds@tycho.nsa.gov> X-Mailer: git-send-email 2.7.4 X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: Cc: Stephen Smalley MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP The realpath logic in selinux_restorecon() was taken from the Android libselinux fork. However, bionic dirname() and basename() do not modify their argument and therefore are safe to call on a const string. POSIX dirname() and basename() can modify their argument. There is a GNU basename() that does not modify its argument, but not for dirname(). For portability, create copies of the original pathname for each call and keep them around until finished using the result. Fixes "restorecon -r goes up the tree?" bug reported by Jason Zaman. Reported-by: Jason Zaman Signed-off-by: Stephen Smalley --- libselinux/src/selinux_restorecon.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c index 0945138..a3f7d83 100644 --- a/libselinux/src/selinux_restorecon.c +++ b/libselinux/src/selinux_restorecon.c @@ -797,25 +797,38 @@ int selinux_restorecon(const char *pathname_orig, * realpath of containing dir, then appending last component name. */ if (flags.userealpath) { - pathbname = basename((char *)pathname_orig); + char *basename_cpy = strdup(pathname_orig); + if (!basename_cpy) + return -1; + pathbname = basename(basename_cpy); if (!strcmp(pathbname, "/") || !strcmp(pathbname, ".") || !strcmp(pathbname, "..")) { pathname = realpath(pathname_orig, NULL); - if (!pathname) + if (!pathname) { + free(basename_cpy); goto realpatherr; + } } else { - pathdname = dirname((char *)pathname_orig); + char *dirname_cpy = strdup(pathname_orig); + pathdname = dirname(dirname_cpy); pathdnamer = realpath(pathdname, NULL); - if (!pathdnamer) + if (!pathdnamer) { + free(basename_cpy); + free(dirname_cpy); goto realpatherr; + } if (!strcmp(pathdnamer, "/")) error = asprintf(&pathname, "/%s", pathbname); else error = asprintf(&pathname, "%s/%s", pathdnamer, pathbname); - if (error < 0) + free(dirname_cpy); + if (error < 0) { + free(basename_cpy); goto oom; + } } + free(basename_cpy); } else { pathname = strdup(pathname_orig); if (!pathname)