From patchwork Wed Oct 5 14:59:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Smalley X-Patchwork-Id: 9363081 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 E3713600C8 for ; Wed, 5 Oct 2016 14:59:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D45B12893D for ; Wed, 5 Oct 2016 14:59:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C8B752899C; Wed, 5 Oct 2016 14:59:01 +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=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from emsm-gh1-uea10.nsa.gov (smtp.nsa.gov [8.44.101.8]) (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 B5E682893D for ; Wed, 5 Oct 2016 14:58:58 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.31,449,1473120000"; d="scan'208";a="18306766" IronPort-PHdr: =?us-ascii?q?9a23=3Abd+nXhy/EFpIn3DXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?0OoVIJqq85mqBkHD//Il1AaPBtSBrakcwLqP+4nbGkU4qa6bt34DdJEeHzQksu?= =?us-ascii?q?4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2WVTerzWI4CIIHV2nbEwu?= =?us-ascii?q?d76zQdaZ353//tvx0qWbWx9Piju5bOE6BzSNhiKViPMrh5B/IL060BrDrygAUe?= =?us-ascii?q?1XwWR1OQDbxE6ktY+N5porzwB887JkrZYBAu3GePEjQLhZCik2G3wk783s8x/Y?= =?us-ascii?q?RE2A4WVYGn8XmRpBGQTy5yb6V5b3vzDSvPZ83jWXJ8v7UfY/XjHmp6ViUxjuoD?= =?us-ascii?q?8KLTA49mDQjIp7i6cIjgimoklE34PMYIyTfMF7d6fZcMJSEXFNRe5NRidBBcW6?= =?us-ascii?q?dIJJAO0fa7UL57LhrkcD+EPtTTKnA/nin3oS3nI=3D?= X-IPAS-Result: =?us-ascii?q?A2H5AwAJE/VX/wHyM5BcGgEBAQECAQEBAQgBAQEBFgEBAQM?= =?us-ascii?q?BAQEJAQEBgxIBAQEBAR6BU6MKAQEBAQEBBpEGhj4kh3hMAQEBAQEBAQECAQJbJ?= =?us-ascii?q?4IyBAMTBYIYAjcUIAsDAwkCFykICAMBLRUYBwsFGASILbkuDAElhXKJOBEBhXo?= =?us-ascii?q?FjjeLQ493Aol9hXRIkClUPwUHhHVWhhZ4gSgBAQE?= Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP; 05 Oct 2016 14:58:02 +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 u95EvQj6004841; Wed, 5 Oct 2016 10:57:35 -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 u95EvOLh165669 for ; Wed, 5 Oct 2016 10:57:24 -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 u95EvM6v004832; Wed, 5 Oct 2016 10:57:22 -0400 From: Stephen Smalley To: selinux@tycho.nsa.gov, richard_c_haines@btinternet.com, jason@perfinion.com Subject: [PATCH v2] libselinux: selinux_restorecon: fix realpath logic Date: Wed, 5 Oct 2016 10:59:34 -0400 Message-Id: <1475679574-27559-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 --- v2 fixes some of the error paths and moves up the freeing of the dirname_cpy to the earliest point possible. libselinux/src/selinux_restorecon.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c index 0945138..e38d1d0 100644 --- a/libselinux/src/selinux_restorecon.c +++ b/libselinux/src/selinux_restorecon.c @@ -797,25 +797,41 @@ 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) + goto realpatherr; + 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); + if (!dirname_cpy) { + free(basename_cpy); + goto realpatherr; + } + pathdname = dirname(dirname_cpy); pathdnamer = realpath(pathdname, NULL); - if (!pathdnamer) + free(dirname_cpy); + if (!pathdnamer) { + free(basename_cpy); goto realpatherr; + } if (!strcmp(pathdnamer, "/")) error = asprintf(&pathname, "/%s", pathbname); else error = asprintf(&pathname, "%s/%s", pathdnamer, pathbname); - if (error < 0) + if (error < 0) { + free(basename_cpy); goto oom; + } } + free(basename_cpy); } else { pathname = strdup(pathname_orig); if (!pathname)