Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp1208817ybc; Sat, 16 Nov 2019 17:22:14 -0800 (PST) X-Google-Smtp-Source: APXvYqxs82llpBmIjtq2+RPCLmno/0hcteMaNlY1lpXW+Gl6HCVnCnmAl8YwZXiiFew2kDNMpi3a X-Received: by 2002:a17:906:e82:: with SMTP id p2mr14024012ejf.237.1573953734883; Sat, 16 Nov 2019 17:22:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573953734; cv=none; d=google.com; s=arc-20160816; b=mVaRMs2dC5Qqxvy9N/VrScJWac1GKAXY9rT05v3Ii8/CO0u6HTyJG1B1sUUUDLFgDM buCvrq25EuDLuyXJll3NDZryuxgM1zzop14N/yFTTfqhOZxMn2fxKtrghicNNfvCaRkg kDfmxNXSM6++CdLw59+GJMazJUuhU9ZJOaPaUJOPqmpabNuijOjJPzX++ybmlNxR/Oln JjkS2eRP36+XR89aeKBNJC7P8GYB+bnyvSnbgfa+CiOarurxxdzbhHRerTRA6qRaXp/e cEdng8o6vvqjrwcvrdBpdcfWzIIIQckiKsdFH0luhGAZUBCyREiXTF2k30J5W/zvCzWX p5cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MDoyLDX0jpszojJM7Z09EhcoBhzCfqO0NvREQa+E1KE=; b=TSEQgnfnoxr1u4kg/dUyWKZJ1Y9FjPtdLjQ1BHrpC7yfq80TtiwuCH9gMXLeRAXHb2 bz/ZIDZVbV25nL1Ku2QuNuBKA3yIoPn5XYnSmlxTKS9gjRxreT50JY1uIc/kNIW8g5qp P97sfqX/8eTKyse7DwQx6CEDYYtcfq8IzWvlELVLhY52xbzMW5yRfukni8PVXcNS70kH Uu51kJin+mtrY38A+hIA7rCbtHbGA5jbG/xG55ucVvNP82DBqIZgOIji0Z2U24ugzN0f UQoQatDH5ocExfAe63CXibYDzcGgzcRP9ZVCS3XEB6Dsed1AnOHLeQEzP/+bBkoFRJU8 HfUw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id nw21si6161803ejb.222.2019.11.16.17.21.50; Sat, 16 Nov 2019 17:22:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727186AbfKQBU2 (ORCPT + 99 others); Sat, 16 Nov 2019 20:20:28 -0500 Received: from mout-p-102.mailbox.org ([80.241.56.152]:32466 "EHLO mout-p-102.mailbox.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725999AbfKQBU1 (ORCPT ); Sat, 16 Nov 2019 20:20:27 -0500 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 47FvQt249yzKmZB; Sun, 17 Nov 2019 02:20:22 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id L1VPM9iqCDpm; Sun, 17 Nov 2019 02:20:17 +0100 (CET) From: Aleksa Sarai To: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells , Shuah Khan , Shuah Khan , Ingo Molnar , Peter Zijlstra , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , Jonathan Corbet Cc: Aleksa Sarai , Christian Brauner , Linus Torvalds , Eric Biederman , Andy Lutomirski , Andrew Morton , Kees Cook , Jann Horn , Tycho Andersen , David Drysdale , Chanho Min , Oleg Nesterov , Rasmus Villemoes , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Christian Brauner , Aleksa Sarai , dev@opencontainers.org, containers@lists.linux-foundation.org, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-alpha@vger.kernel.org, linux-api@vger.kernel.org, libc-alpha@sourceware.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ia64@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, linux-xtensa@linux-xtensa.org, sparclinux@vger.kernel.org Subject: [PATCH v17 05/13] namei: LOOKUP_NO_SYMLINKS: block symlink resolution Date: Sun, 17 Nov 2019 12:17:05 +1100 Message-Id: <20191117011713.13032-6-cyphar@cyphar.com> In-Reply-To: <20191117011713.13032-1-cyphar@cyphar.com> References: <20191117011713.13032-1-cyphar@cyphar.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org /* Background. */ Userspace cannot easily resolve a path without resolving symlinks, and would have to manually resolve each path component with O_PATH and O_NOFOLLOW. This is clearly inefficient, and can be fairly easy to screw up (resulting in possible security bugs). Linus has mentioned that Git has a particular need for this kind of flag[1]. It also resolves a fairly long-standing perceived deficiency in O_NOFOLLOw -- that it only blocks the opening of trailing symlinks. This is part of a refresh of Al's AT_NO_JUMPS patchset[2] (which was a variation on David Drysdale's O_BENEATH patchset[3], which in turn was based on the Capsicum project[4]). /* Userspace API. */ LOOKUP_NO_SYMLINKS will be exposed to userspace through openat2(2). /* Semantics. */ Unlike most other LOOKUP flags (most notably LOOKUP_FOLLOW), LOOKUP_NO_SYMLINKS applies to all components of the path. With LOOKUP_NO_SYMLINKS, any symlink path component encountered during path resolution will yield -ELOOP. If the trailing component is a symlink (and no other components were symlinks), then O_PATH|O_NOFOLLOW will not error out and will instead provide a handle to the trailing symlink -- without resolving it. /* Testing. */ LOOKUP_NO_SYMLINKS is tested as part of the openat2(2) selftests. [1]: https://lore.kernel.org/lkml/CA+55aFyOKM7DW7+0sdDFKdZFXgptb5r1id9=Wvhd8AgSP7qjwQ@mail.gmail.com/ [2]: https://lore.kernel.org/lkml/20170429220414.GT29622@ZenIV.linux.org.uk/ [3]: https://lore.kernel.org/lkml/1415094884-18349-1-git-send-email-drysdale@google.com/ [4]: https://lore.kernel.org/lkml/1404124096-21445-1-git-send-email-drysdale@google.com/ Cc: Christian Brauner Suggested-by: Al Viro Suggested-by: Linus Torvalds Signed-off-by: Aleksa Sarai --- fs/namei.c | 3 +++ include/linux/namei.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 74574a69a614..303731935eb2 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1052,6 +1052,9 @@ const char *get_link(struct nameidata *nd) int error; const char *res; + if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS)) + return ERR_PTR(-ELOOP); + if (!(nd->flags & LOOKUP_RCU)) { touch_atime(&last->link); cond_resched(); diff --git a/include/linux/namei.h b/include/linux/namei.h index 758e9b47db6f..0d86e75c04a7 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -39,6 +39,9 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND}; #define LOOKUP_ROOT 0x2000 #define LOOKUP_ROOT_GRABBED 0x0008 +/* Scoping flags for lookup. */ +#define LOOKUP_NO_SYMLINKS 0x010000 /* No symlink crossing. */ + extern int path_pts(struct path *path); extern int user_path_at_empty(int, const char __user *, unsigned, struct path *, int *empty); -- 2.24.0