Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2854971lqt; Tue, 23 Apr 2024 03:54:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVUvduqS4M/TEwrbsKAh7cbO/Xc8AmEC8ek5wN7LkgM7Vm8/Q6Pd1cGDBtjCL3jLN/3q/mpt8XsDHq/vX+Of0ou/usBrMXMVbgbUABOQg== X-Google-Smtp-Source: AGHT+IFY4laloMJI1Mm0pDD5RR10dbSjrzgAQqiTaU+NtkBflDXj2/hSJ3VH2FF3VMw34kLIEsbh X-Received: by 2002:a05:620a:5643:b0:78e:cf14:f89a with SMTP id vw3-20020a05620a564300b0078ecf14f89amr3639922qkn.9.1713869661645; Tue, 23 Apr 2024 03:54:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713869661; cv=pass; d=google.com; s=arc-20160816; b=RuyWLNaSwMjaKBzToNrvuM5PN6TDdrn3M1RdnCu/5DxTjZWl4RPrAQ/6uCsRz8891p NBa1BJt2x/45yCQU096YJhGmbCtPHnbClsKntK/+j3J0G3yO+83qbBshoeWUos5I5TQ0 axG7pfcRu1ZtC+urw6mngWNi0NvAxYT/UyE58AWgvrPd34nzfrt1MfQVT/LyT4uctHKN QyY/sdcwOPK2JAOAZME9z2glIM6QtMOaA8ADzkxZVqWAiue9oq4ZG7M86E7V8pEsnyit MmfLTacIrgS8/4yDFRrPFIAOVgp6ENPANytBWEYEdr1RtKTg6WJl2joBzfq+7LZ06EcL mMBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ehiAOGcVzI3bx2ma+GbYdge1RWRGLiGOpmC8xlK5PQE=; fh=g5S+5xWGh1LSoCH6S0g+mSoCWam8q4GLMeWghGKi028=; b=a296buQF1A35SnQcSLr6xNv4Crf9fIZu1W+PU7p7KwtLywY6+WFLrpDKusAZ/xWmMw rS30SkPn+9tMP03mllJb88rr/1K/pyQNoRtaNI1hREMahVRtuODYAFIQn2WszUQXvL9K jgQ+Lo4VC+E/3sdWXkSSJSBzCiF6DcdnrS/eYsZbyZfinDodDozemRvCTAWKo98DkUiZ c07obZLCo4mgay/r+j7ne+AP0AAfPdJK+6qwLMIFGPTyg1OWF8RJsmnJa7IvV+BUkDgw hrsfXJjig0Uq07l8AhEZemENrH2DaWLFy2qUoX1JL+szjKQigGzzXRmpZzgSTbNSFZuz wN/w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@yandex.ru header.s=mail header.b=vtrKtpWh; arc=pass (i=1 spf=pass spfdomain=yandex.ru dkim=pass dkdomain=yandex.ru dmarc=pass fromdomain=yandex.ru); spf=pass (google.com: domain of linux-kernel+bounces-154922-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ye23-20020a05620a3b9700b007906d0eae74si5666631qkn.149.2024.04.23.03.54.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 03:54:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154922-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@yandex.ru header.s=mail header.b=vtrKtpWh; arc=pass (i=1 spf=pass spfdomain=yandex.ru dkim=pass dkdomain=yandex.ru dmarc=pass fromdomain=yandex.ru); spf=pass (google.com: domain of linux-kernel+bounces-154922-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3D20A1C21946 for ; Tue, 23 Apr 2024 10:54:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 792B684E02; Tue, 23 Apr 2024 10:49:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=yandex.ru header.i=@yandex.ru header.b="vtrKtpWh" Received: from forward101c.mail.yandex.net (forward101c.mail.yandex.net [178.154.239.212]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2DFA7E564; Tue, 23 Apr 2024 10:49:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.212 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713869378; cv=none; b=J7Jx66xukZUz/usUHdrb1/yjvUkTQyseSSetZvRez7z79wQk1ZYCPv/vJAWZoFS7EN99/7ZDOVgBwlcVX6yYK3A8pWtxW5AQS7DLN+KsqEbcBx7j/DnUxRlGvHiCADUA5ZZhdyfJmawNaA/mSu4UISkc4fvvThUCQBkLa0R0FCU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713869378; c=relaxed/simple; bh=+OSMikil93STCKOPtNMLQAgzOlKbqGJFnnhDVohp4t0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jCDrRSBuHf2TK2UT7p4Ux/lCLaiblr7WyKdzX5rUBTulmH29RAJ4aF8hLjc97RS9YlBCRahxihXxMprKurcoHsupgHLxEH7RIMWZW8t188qpCwTq/PSuo/BagnEIvMKAfUjhKKZJpci/gPs/SxAXuXp/q+FAYEMHr0F0x+5uz9o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex.ru; spf=pass smtp.mailfrom=yandex.ru; dkim=pass (1024-bit key) header.d=yandex.ru header.i=@yandex.ru header.b=vtrKtpWh; arc=none smtp.client-ip=178.154.239.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=yandex.ru Received: from mail-nwsmtp-smtp-production-main-85.iva.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-85.iva.yp-c.yandex.net [IPv6:2a02:6b8:c0c:b1a0:0:640:e983:0]) by forward101c.mail.yandex.net (Yandex) with ESMTPS id 311D360AC0; Tue, 23 Apr 2024 13:49:34 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-85.iva.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id TnHoiCGOm4Y0-QYddym58; Tue, 23 Apr 2024 13:49:33 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1713869373; bh=ehiAOGcVzI3bx2ma+GbYdge1RWRGLiGOpmC8xlK5PQE=; h=Cc:Message-ID:References:Date:In-Reply-To:Subject:To:From; b=vtrKtpWhdONYwM8XXk4GdKNdGW3mjjjD513zMxLqKYFmk0FOzcajwcTzKNdciiqPw Ft59IfFxy9CPRttnbqBT4ZjontjO3X7o70Px6ME3Uy659KFlx8NhNGRNBjie14ObmN ythJzp+4zPVGJmzAixhVbah7UxN5J5hGqxtWFKb8= Authentication-Results: mail-nwsmtp-smtp-production-main-85.iva.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Stas Sergeev To: linux-kernel@vger.kernel.org Cc: Stas Sergeev , Stefan Metzmacher , Eric Biederman , Alexander Viro , Andy Lutomirski , Christian Brauner , Jan Kara , Jeff Layton , Chuck Lever , Alexander Aring , linux-fsdevel@vger.kernel.org, Paolo Bonzini , =?UTF-8?q?Christian=20G=C3=B6ttsche?= Subject: [PATCH 2/2] openat2: add OA2_INHERIT_CRED flag Date: Tue, 23 Apr 2024 13:48:24 +0300 Message-ID: <20240423104824.10464-3-stsp2@yandex.ru> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423104824.10464-1-stsp2@yandex.ru> References: <20240423104824.10464-1-stsp2@yandex.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This flag performs the open operation with the credentials that were in effect when dir_fd was opened. This allows the process to pre-open some directories and then change eUID (and all other UIDs/GIDs) to a less-privileged user, retaining the ability to open/create files within these directories. Design goal: The idea is to provide a very light-weight sandboxing, where the process, without the use of any heavy-weight techniques like chroot within namespaces, can restrict the access to the set of pre-opened directories. This patch is just a first step to such sandboxing. If things go well, in the future the same extension can be added to more syscalls. These should include at least unlinkat(), renameat2() and the not-yet-upstreamed setxattrat(). Security considerations: To avoid sandboxing escape, this patch makes sure the restricted lookup modes are used. Namely, RESOLVE_BENEATH or RESOLVE_IN_ROOT. To avoid leaking creds across exec, this patch requires O_CLOEXEC flag on a directory. Use cases: Virtual machines that deal with untrusted code, can use that instead of a more heavy-weighted approaches. Currently the approach is being tested on a dosemu2 VM. Signed-off-by: Stas Sergeev CC: Stefan Metzmacher CC: Eric Biederman CC: Alexander Viro CC: Andy Lutomirski CC: Christian Brauner CC: Jan Kara CC: Jeff Layton CC: Chuck Lever CC: Alexander Aring CC: linux-fsdevel@vger.kernel.org CC: linux-kernel@vger.kernel.org CC: Paolo Bonzini CC: Christian Göttsche --- fs/internal.h | 2 +- fs/namei.c | 30 ++++++++++++++++++++++++++++-- fs/open.c | 2 +- include/linux/fcntl.h | 2 ++ include/uapi/linux/openat2.h | 3 +++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/fs/internal.h b/fs/internal.h index 7ca738904e34..692b53b19aad 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -169,7 +169,7 @@ static inline void sb_end_ro_state_change(struct super_block *sb) * open.c */ struct open_flags { - int open_flag; + u64 open_flag; umode_t mode; int acc_mode; int intent; diff --git a/fs/namei.c b/fs/namei.c index 2fde2c320ae9..0e0f2e32ef02 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -586,6 +586,7 @@ struct nameidata { int dfd; vfsuid_t dir_vfsuid; umode_t dir_mode; + const struct cred *dir_open_cred; } __randomize_layout; #define ND_ROOT_PRESET 1 @@ -695,6 +696,7 @@ static void terminate_walk(struct nameidata *nd) nd->depth = 0; nd->path.mnt = NULL; nd->path.dentry = NULL; + put_cred(nd->dir_open_cred); } /* path_put is needed afterwards regardless of success or failure */ @@ -2414,6 +2416,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags) get_fs_pwd(current->fs, &nd->path); nd->inode = nd->path.dentry->d_inode; } + nd->dir_open_cred = get_current_cred(); } else { /* Caller must check execute permissions on the starting path component */ struct fd f = fdget_raw(nd->dfd); @@ -2437,6 +2440,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags) path_get(&nd->path); nd->inode = nd->path.dentry->d_inode; } + nd->dir_open_cred = get_cred(f.file->f_cred); fdput(f); } @@ -3794,8 +3798,28 @@ static struct file *path_openat(struct nameidata *nd, error = do_o_path(nd, flags, file); } else { const char *s = path_init(nd, flags); - file = alloc_empty_file(op->open_flag, current_cred()); - error = PTR_ERR_OR_ZERO(file); + const struct cred *old_cred = NULL; + + error = 0; + if (op->open_flag & OA2_INHERIT_CRED) { + /* Make sure to work only with restricted + * look-up modes. + */ + if (!(nd->flags & (LOOKUP_BENEATH | LOOKUP_IN_ROOT))) + error = -EPERM; + /* Only work with O_CLOEXEC dirs. */ + if (!get_close_on_exec(nd->dfd)) + error = -EPERM; + + if (!error) + old_cred = override_creds(nd->dir_open_cred); + } + if (!error) { + file = alloc_empty_file(op->open_flag, current_cred()); + error = PTR_ERR_OR_ZERO(file); + } else { + file = ERR_PTR(error); + } if (!error) { while (!(error = link_path_walk(s, nd)) && (s = open_last_lookups(nd, file, op)) != NULL) @@ -3803,6 +3827,8 @@ static struct file *path_openat(struct nameidata *nd, } if (!error) error = do_open(nd, file, op); + if (old_cred) + revert_creds(old_cred); terminate_walk(nd); if (IS_ERR(file)) return file; diff --git a/fs/open.c b/fs/open.c index ee8460c83c77..6be013182a35 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1225,7 +1225,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) * values before calling build_open_flags(), but openat2(2) checks all * of its arguments. */ - if (flags & ~VALID_OPEN_FLAGS) + if (flags & ~VALID_OPENAT2_FLAGS) return -EINVAL; if (how->resolve & ~VALID_RESOLVE_FLAGS) return -EINVAL; diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h index a332e79b3207..b71f8b162102 100644 --- a/include/linux/fcntl.h +++ b/include/linux/fcntl.h @@ -12,6 +12,8 @@ FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \ O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE) +#define VALID_OPENAT2_FLAGS (VALID_OPEN_FLAGS | OA2_INHERIT_CRED) + /* List of all valid flags for the how->resolve argument: */ #define VALID_RESOLVE_FLAGS \ (RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \ diff --git a/include/uapi/linux/openat2.h b/include/uapi/linux/openat2.h index a5feb7604948..cdd676a10b62 100644 --- a/include/uapi/linux/openat2.h +++ b/include/uapi/linux/openat2.h @@ -40,4 +40,7 @@ struct open_how { return -EAGAIN if that's not possible. */ +/* openat2-specific flags go to upper 4 bytes. */ +#define OA2_INHERIT_CRED (1ULL << 32) + #endif /* _UAPI_LINUX_OPENAT2_H */ -- 2.44.0