From patchwork Thu Oct 6 08:27:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13000049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA568C433F5 for ; Thu, 6 Oct 2022 08:27:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 377506B0073; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FF4D6B0074; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 104276B0075; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id ED13A6B0073 for ; Thu, 6 Oct 2022 04:27:43 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B65821406A1 for ; Thu, 6 Oct 2022 08:27:43 +0000 (UTC) X-FDA: 79989846006.09.F5EB549 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf09.hostedemail.com (Postfix) with ESMTP id 5CCB5140017 for ; Thu, 6 Oct 2022 08:27:42 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id l4so1070776plb.8 for ; Thu, 06 Oct 2022 01:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=6chMDy7sdAp7fbcB3rx+WX3mNPHiNX0s/cFRhM3ieOg=; b=LEDgMUcWBRzmi9Mo0+9qOenVfeL77iuZJWbz/l6KBHO9OnXx6oo4g63CPC4mRrRTek VdFOJa1Yb5jMjiimaPf2n+eeRbuQO4DDWl5V+/M07tajLIckBZFpcXq+ubCsS3ZX/aht M8noy9tEF8k2Cvub9V50VkmCeEeuaJzybH8cY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=6chMDy7sdAp7fbcB3rx+WX3mNPHiNX0s/cFRhM3ieOg=; b=lfW5PUb9+p5ASDyaiFOCi8JKHk4fycAqRLqzsxkK4SqJll8+9FNI/sgpxcn80fL1Nh 4Td0DLnAL6+J2x8xOz3uPQbCm4MkK4jWuDH80HAFouNiZkYM0sC4g27Or9/nWN93c1TH onsPsxajU8lYDskdr9/jFROYou3KYPJM1bBtUuct/+wMNJhnbwcqums+KkfoiTL5WLbF D96/BGiMucRGClggd/l6Ol9fIGyd8t+ajds1cxwuaS92ZxRvZe2w9u+3MM3CcQLr+ZzH nsi67UbG7HbcEwSH3wIm4LVvDQh3lAGGWGj9+TAzg7Kqn4f1y2yKhJ48Gzz7w27cdN7E vVxg== X-Gm-Message-State: ACrzQf0o5LSLHmQdf09UX1oEzhR2JwQwwEdaOW9oLttofTfunCg/8tR9 YRSGvxIMdSTbg6TdEmHRN3QgVg== X-Google-Smtp-Source: AMsMyM651YYuwVyNy7b2zv0p60OW0W4RrTSUf6wg1C/eAbw9LtHIICPLS8y24ano7++OQg+BAuWoUA== X-Received: by 2002:a17:90b:4b03:b0:20a:c33f:ca47 with SMTP id lx3-20020a17090b4b0300b0020ac33fca47mr4129164pjb.10.1665044861286; Thu, 06 Oct 2022 01:27:41 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k6-20020a170902c40600b0017f8094a52asm2330200plk.29.2022.10.06.01.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 01:27:39 -0700 (PDT) From: Kees Cook To: Eric Biederman Cc: Kees Cook , Jorge Merlino , Alexander Viro , "Christian Brauner (Microsoft)" , Thomas Gleixner , Andy Lutomirski , Sebastian Andrzej Siewior , Andrew Morton , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, John Johansen , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Eric Paris , Richard Haines , Casey Schaufler , Xin Long , "David S. Miller" , Todd Kjos , Ondrej Mosnacek , Prashanth Prahlad , Micah Morton , Fenghua Yu , Andrei Vagin , linux-kernel@vger.kernel.org, apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] fs/exec: Explicitly unshare fs_struct on exec Date: Thu, 6 Oct 2022 01:27:34 -0700 Message-Id: <20221006082735.1321612-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221006082735.1321612-1-keescook@chromium.org> References: <20221006082735.1321612-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=8132; h=from:subject; bh=pn442Q+K4NVJzK4Ogn1OJE0Kf/fr+fLN7d/sv7w7wIQ=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjPpF3G5PQJoJ0+WU4egcCbptwwSoJJwtYzGLz8/uw x6+wiOSJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYz6RdwAKCRCJcvTf3G3AJjwwD/ 9AqmiFkR2fsSZYPEIflwSZT7OI5y0/NnvllzZijYCc4qJTyuATyZSV+rAw52JGZI+UqhwIWo7Rw3tL 0W6e8cdMFRts0lRnFAPBRbdCsvl/YhcO97MwHkXJiO8t7lYOJ49tSGrhJYFwb3zq1CihY8b5VjSGVK ynVnjgw0Aoj4C7QEy470dR/keFQc7ubnCJGalsUwSmj3oT/WleAcuy4/N4YretzlLd/s0OYeU7oGzc CP/TmfFOPNaB4UkhfILY+ZcowRsqvPz4GX1CF0syEVrwtJcmQd5H8N60VDdQfc4Ge2p07/ysSRog3W VSb84rDk8sqk3Vx3lj3cK2Z7Z60xZZ7cG9+PsJjD6EasD3k/3MQAOzFZLqlhMr72dSbuYf3V1M70xJ 8dliwMLT2aH74NOjxeLjMWsO+JJYjnmOabVIvFRzSTU3zlNwm0vu7JJX6W/MAfHjtVsDCDyDde4c3A Kqn9wIBt2UjMVZCorgxKTKEj8LHwC5OdxCNdtbIAnB4YqGdsNmQbWpwX45m4i6Yl3XLbFVbSzNT5xC 3939YiCArJArhAqIkw8Fk0jRBqH1ogvt1e6kNTuu94/O5w/TRidPhivZndR2gjgX4PTr5duo/9VGSq kciNr8P/ei//btCcC+4kU3tOLQ15QtK11gc5XyK8JReuFgDI99wTCnGabozQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665044862; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6chMDy7sdAp7fbcB3rx+WX3mNPHiNX0s/cFRhM3ieOg=; b=NVwExl8IgKMX3ZXBYfu5csioc+wqFVlYKeengbFBkO9QCpcePm93vW0PmvKf6gDA0eAaH2 lKExS0uWqIu1fhxcws8u4yAmnKLGeabNr4GxzbokOK53QgcvobIfqd0nJcKyp2jKK6d6bc EqxkjHcDVXu/pXmyfCL8sQIM4M/G6IQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=LEDgMUcW; spf=pass (imf09.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.172 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665044862; a=rsa-sha256; cv=none; b=CjUdU1V6ohjRZ6TFoRtbeX8Ilt0XD6t/kbjwwux00jh3rk8UFrVEtqJb58XDBy9er0knog VEQFML0CAHXlCf6kkriN+5Omv6C9bAWcGp7+6CNPS2tmZFiy8F8csl78vqa5laLGekcLnY A2kgdpN4uGDIpgKJXdDOS5cZTWyLHgU= Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=LEDgMUcW; spf=pass (imf09.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.172 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 5CCB5140017 X-Rspam-User: X-Stat-Signature: tyeegtmqa7h7y5fji497jzisobax4ok5 X-HE-Tag: 1665044862-423948 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The check_unsafe_exec() counting of n_fs would not add up under a heavily threaded process trying to perform a suid exec, causing the suid portion to fail. This counting error appears to be unneeded, but to catch any possible conditions, explicitly unshare fs_struct on exec, if it ends up needing to happen. This means fs->in_exec must be removed (since it would never get cleared in the old copy), and is specifically no longer needed. See also commit 498052bba55e ("New locking/refcounting for fs_struct"). This additionally allows the "in_exec" member to be dropped, showing an 8 bytes savings per fs_struct on 64-bit. Before: struct fs_struct { int users; /* 0 4 */ spinlock_t lock; /* 4 4 */ seqcount_spinlock_t seq; /* 8 4 */ int umask; /* 12 4 */ int in_exec; /* 16 4 */ /* XXX 4 bytes hole, try to pack */ struct path root; /* 24 16 */ struct path pwd; /* 40 16 */ /* size: 56, cachelines: 1, members: 7 */ /* sum members: 52, holes: 1, sum holes: 4 */ /* last cacheline: 56 bytes */ }; After: struct fs_struct { int users; /* 0 4 */ spinlock_t lock; /* 4 4 */ seqcount_spinlock_t seq; /* 8 4 */ int umask; /* 12 4 */ struct path root; /* 16 16 */ struct path pwd; /* 32 16 */ /* size: 48, cachelines: 1, members: 6 */ /* last cacheline: 48 bytes */ }; Reported-by: Jorge Merlino Link: https://lore.kernel.org/lkml/20220910211215.140270-1-jorge.merlino@canonical.com/ Cc: Eric Biederman Cc: Alexander Viro Cc: "Christian Brauner (Microsoft)" Cc: Thomas Gleixner Cc: Andy Lutomirski Cc: Sebastian Andrzej Siewior Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Kees Cook --- fs/exec.c | 9 +++--- fs/fs_struct.c | 1 - include/linux/fdtable.h | 1 + include/linux/fs_struct.h | 1 - kernel/fork.c | 62 ++++++++++++++++++++++++++------------- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 902bce45b116..7d5f63f03c58 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1272,6 +1272,11 @@ int begin_new_exec(struct linux_binprm * bprm) if (retval) goto out; + /* Ensure the fs_struct is not shared. */ + retval = unshare_fs(); + if (retval) + goto out; + /* * Must be called _before_ exec_mmap() as bprm->mm is * not visible until then. This also enables the update @@ -1583,8 +1588,6 @@ static void check_unsafe_exec(struct linux_binprm *bprm) if (p->fs->users > n_fs) bprm->unsafe |= LSM_UNSAFE_SHARE; - else - p->fs->in_exec = 1; spin_unlock(&p->fs->lock); } @@ -1843,7 +1846,6 @@ static int bprm_execve(struct linux_binprm *bprm, goto out; /* execve succeeded */ - current->fs->in_exec = 0; current->in_execve = 0; rseq_execve(current); acct_update_integrals(current); @@ -1861,7 +1863,6 @@ static int bprm_execve(struct linux_binprm *bprm, force_fatal_sig(SIGSEGV); out_unmark: - current->fs->in_exec = 0; current->in_execve = 0; return retval; diff --git a/fs/fs_struct.c b/fs/fs_struct.c index 04b3f5b9c629..c27c67023d01 100644 --- a/fs/fs_struct.c +++ b/fs/fs_struct.c @@ -115,7 +115,6 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old) /* We don't need to lock fs - think why ;-) */ if (fs) { fs->users = 1; - fs->in_exec = 0; spin_lock_init(&fs->lock); seqcount_spinlock_init(&fs->seq, &fs->lock); fs->umask = old->umask; diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index e066816f3519..fbfb89ee3bda 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -117,6 +117,7 @@ struct task_struct; void put_files_struct(struct files_struct *fs); int unshare_files(void); +int unshare_fs(void); struct files_struct *dup_fd(struct files_struct *, unsigned, int *) __latent_entropy; void do_close_on_exec(struct files_struct *); int iterate_fd(struct files_struct *, unsigned, diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h index 783b48dedb72..08b82a90ceae 100644 --- a/include/linux/fs_struct.h +++ b/include/linux/fs_struct.h @@ -11,7 +11,6 @@ struct fs_struct { spinlock_t lock; seqcount_spinlock_t seq; int umask; - int in_exec; struct path root, pwd; } __randomize_layout; diff --git a/kernel/fork.c b/kernel/fork.c index b4a799d9c50f..53b7248f7a4b 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1589,10 +1589,6 @@ static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) if (clone_flags & CLONE_FS) { /* tsk->fs is already what we want */ spin_lock(&fs->lock); - if (fs->in_exec) { - spin_unlock(&fs->lock); - return -EAGAIN; - } fs->users++; spin_unlock(&fs->lock); return 0; @@ -3070,10 +3066,9 @@ static int check_unshare_flags(unsigned long unshare_flags) return 0; } -/* - * Unshare the filesystem structure if it is being shared - */ -static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) +/* Allocate an fs_struct if it is currently shared and CLONE_FS requested. */ +static int unshare_fs_alloc(unsigned long unshare_flags, + struct fs_struct **new_fsp) { struct fs_struct *fs = current->fs; @@ -3091,6 +3086,41 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) return 0; } +/* Swap out fs_struct, returning old fs if it needs freeing. */ +static void unshare_fs_finalize(struct fs_struct **new_fsp) +{ + struct task_struct *task = current; + struct fs_struct *fs = task->fs; + + spin_lock(&fs->lock); + task->fs = *new_fsp; + if (--fs->users) + *new_fsp = NULL; + else + *new_fsp = fs; + spin_unlock(&fs->lock); +} + +/* + * Unshare the filesystem structure if it is being shared + */ +int unshare_fs(void) +{ + struct fs_struct *new_fs = NULL; + int error; + + error = unshare_fs_alloc(CLONE_FS, &new_fs); + if (error || !new_fs) + return error; + + unshare_fs_finalize(&new_fs); + + if (new_fs) + free_fs_struct(new_fs); + + return 0; +} + /* * Unshare file descriptor table if it is being shared */ @@ -3120,7 +3150,7 @@ int unshare_fd(unsigned long unshare_flags, unsigned int max_fds, */ int ksys_unshare(unsigned long unshare_flags) { - struct fs_struct *fs, *new_fs = NULL; + struct fs_struct *new_fs = NULL; struct files_struct *new_fd = NULL; struct cred *new_cred = NULL; struct nsproxy *new_nsproxy = NULL; @@ -3159,7 +3189,7 @@ int ksys_unshare(unsigned long unshare_flags) */ if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) do_sysvsem = 1; - err = unshare_fs(unshare_flags, &new_fs); + err = unshare_fs_alloc(unshare_flags, &new_fs); if (err) goto bad_unshare_out; err = unshare_fd(unshare_flags, NR_OPEN_MAX, &new_fd); @@ -3197,16 +3227,8 @@ int ksys_unshare(unsigned long unshare_flags) task_lock(current); - if (new_fs) { - fs = current->fs; - spin_lock(&fs->lock); - current->fs = new_fs; - if (--fs->users) - new_fs = NULL; - else - new_fs = fs; - spin_unlock(&fs->lock); - } + if (new_fs) + unshare_fs_finalize(&new_fs); if (new_fd) swap(current->files, new_fd); From patchwork Thu Oct 6 08:27:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13000050 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BD3DC43217 for ; Thu, 6 Oct 2022 08:27:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C061A6B0074; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B3C366B0075; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B4D36B0078; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 805EA6B0074 for ; Thu, 6 Oct 2022 04:27:44 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4DA51AB3DF for ; Thu, 6 Oct 2022 08:27:44 +0000 (UTC) X-FDA: 79989846048.08.FC7463D Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf11.hostedemail.com (Postfix) with ESMTP id CCAC240008 for ; Thu, 6 Oct 2022 08:27:41 +0000 (UTC) Received: by mail-pf1-f179.google.com with SMTP id 204so1391169pfx.10 for ; Thu, 06 Oct 2022 01:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=JXTirUT/Csg4APgKb6IoBxbOkY1M1FiylJ4snUxz0UA=; b=MV1Xq/Ck5nDq5FkA2A/UwIV8B30dvCVwZwYBMaY5KkmjN8HzeobdGijbvEPdreTL0I hVhs0leFZmmgwsEcyYJ5LKRD3rksRH/MX05zPgzCuhL2mf5faZobGVDXhr0NK0y3ncts WDg5Irmwiclv1+TuKbuqMlK939w/T3VPQBGLU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=JXTirUT/Csg4APgKb6IoBxbOkY1M1FiylJ4snUxz0UA=; b=2QCIL7Ct0p+APk7OJgaS8euvCvFkTqH6g1fStSAUwaTYA5DboMUZycv1VJPA0x7YAC pUQK7SHMmdCe/q+PNd6PDDOgz6HxMz+Mp28PZHU9Cavbi61+qaU1FAxTeDHT57T89pmZ tF+/vx1dl3nh6kVc5l2wwEgww6EV/53wTuZ0Ag+sV9BTZxfKL4rJ40ldPqhFaIeUmTLK VocFfh0lCb11l72Py+KQjoTssj9CaUS0qaDj5txmU7+rhAr4KV8qt3B4cQqSinWpktT0 JPtrtk6RWhiU70lYQQsVZpQAzLSmWiiMWt4N3F4pfE3WXa2dInTPG5NJsmt7I/Q3wmF4 tKYw== X-Gm-Message-State: ACrzQf3+BGLI/ON9YM9HKVedd2VH6ksTNPNLulNF9rjVuvkzeYwxA4ng /2KtnffMh9WVcKfPbwAHYPdPBg== X-Google-Smtp-Source: AMsMyM6W5XwnXecNI4zSPGpyeZlol/ZTonmHl763803Md18AZocxPoiswNswpf1uUPT4OHuojpgkmA== X-Received: by 2002:a63:d54a:0:b0:454:395a:73d6 with SMTP id v10-20020a63d54a000000b00454395a73d6mr3545058pgi.531.1665044860838; Thu, 06 Oct 2022 01:27:40 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id v9-20020a17090a4ec900b00200b2894648sm2344795pjl.52.2022.10.06.01.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 01:27:39 -0700 (PDT) From: Kees Cook To: Eric Biederman Cc: Kees Cook , Alexander Viro , John Johansen , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Eric Paris , Richard Haines , Casey Schaufler , Xin Long , "David S. Miller" , Todd Kjos , Ondrej Mosnacek , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, Jorge Merlino , "Christian Brauner (Microsoft)" , Thomas Gleixner , Andy Lutomirski , Sebastian Andrzej Siewior , Andrew Morton , Prashanth Prahlad , Micah Morton , Fenghua Yu , Andrei Vagin , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/2] exec: Remove LSM_UNSAFE_SHARE Date: Thu, 6 Oct 2022 01:27:35 -0700 Message-Id: <20221006082735.1321612-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221006082735.1321612-1-keescook@chromium.org> References: <20221006082735.1321612-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3927; h=from:subject; bh=ekDs8TYo6gmnVS2X5P/NZPdmVTJQlJO9uXeDqf2iTeY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjPpF336GBqTJk2qjgX5GvQ3feqQTP3mEtrz77nvir edCOPRuJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYz6RdwAKCRCJcvTf3G3AJpwJEA CMrwF69qIO3OxGZxaGmmHUH3737CPlmPn0pDmdy3Hdb4vdmhacgZe56ZODfZEIOyku5HRei4EgW9Un lzYhSJLyWOrRjwPEVTKM0BlRqY1HItnaiqVWowS5JKvfxL2L9IQpY0QYtxCwvnJ2W0k8AQ+p6+Ki4o F/g2Je0/gx6G4S7AF/T2dVFNrC1I2c87k6eL23rFSo0OQU9LR7LjdrpSZJ30+nBivjJGu5sgi4eHwn pQXXIhLFBaII7ltA8d62vwj7rKBAAeQoRzpY95pA1nciM5YG4B9P8aJHsgdBADxRZbVXQlepYcvdRf VlawFag7VNsY3EDHQsFYuVxU3jfNQ9aWYiQm3IId5UVkzpseGi1cp/VAH10ttHQtajYjO5MIM/tKh4 7ms3y2FKhLnnEdyE3WSdd8mSgawrXjpmeuEpDI2vMZ6BlhuIx3kFGvg0wwXPKmuhKRF/KTQ+Xm7Q1B whkflq3t78dZ8X+TqDC5voTQ0W1A8o3EMLBJCNfs19t51e9ZWpL+GFM2xZlZeUGxY+arOWBjkl68yH hBgpFgFl3+Ti42WCPYzFfu3pVGyV/VYFC3VWIh0k2UTi9ncMlCncCVMVt+w+NSHYYLQzTADe44E3kX fF3/UQMXjAf4VO+FWmoBpvB2CbVa1VW942hoVo6zyef2e+sodtvRAZwUdA1w== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1665044861; a=rsa-sha256; cv=none; b=m5D6TeXzKiq+82UjGAmfG3XZTP9G29seY8a+o/9E9N06Rwt816ObkzwLTErhWbNWOSKl5O h5/y8VG+cVjuDdITlM4yu6xgLWPWwgF/hLdro3EQgfr0L3tQqhaCwE0HSfIHL+lGMK0+rL Yp3n55VC2Wna9hgRbvwziXE8IjYUMzw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="MV1Xq/Ck"; spf=pass (imf11.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1665044861; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JXTirUT/Csg4APgKb6IoBxbOkY1M1FiylJ4snUxz0UA=; b=a4DyQvwpc8kRJApw4aHqc2iKuyluznBBEnu1G/8vQYMNXc+IVEKJnkKquu+1hON0rg4F19 9OTVf+765YNc+80xSSCiCTDPdXn+aQ8JO19ISScK87L9LGOn6WQg4Oku13aH+hvtiLkNA8 W34NXSvvddPEj5fc+MijozVWw8EQb/4= X-Rspamd-Queue-Id: CCAC240008 X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="MV1Xq/Ck"; spf=pass (imf11.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-Stat-Signature: 1mekw85rckiy49ae36fsij7gqyb77tp9 X-Rspamd-Server: rspam04 X-HE-Tag: 1665044861-928183 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: With fs_struct explicitly unshared during exec, it is no longer possible to have unexpected shared state, and LSM_UNSAFE_SHARE can be entirely removed. Cc: Alexander Viro Cc: Eric Biederman Cc: John Johansen Cc: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Eric Paris Cc: Richard Haines Cc: Casey Schaufler Cc: Xin Long Cc: "David S. Miller" Cc: Todd Kjos Cc: Ondrej Mosnacek Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org Cc: apparmor@lists.ubuntu.com Cc: linux-security-module@vger.kernel.org Cc: selinux@vger.kernel.org Signed-off-by: Kees Cook --- fs/exec.c | 17 +---------------- include/linux/security.h | 5 ++--- security/apparmor/domain.c | 5 ----- security/selinux/hooks.c | 10 ---------- 4 files changed, 3 insertions(+), 34 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 7d5f63f03c58..3cd058711098 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1563,8 +1563,7 @@ EXPORT_SYMBOL(bprm_change_interp); */ static void check_unsafe_exec(struct linux_binprm *bprm) { - struct task_struct *p = current, *t; - unsigned n_fs; + struct task_struct *p = current; if (p->ptrace) bprm->unsafe |= LSM_UNSAFE_PTRACE; @@ -1575,20 +1574,6 @@ static void check_unsafe_exec(struct linux_binprm *bprm) */ if (task_no_new_privs(current)) bprm->unsafe |= LSM_UNSAFE_NO_NEW_PRIVS; - - t = p; - n_fs = 1; - spin_lock(&p->fs->lock); - rcu_read_lock(); - while_each_thread(p, t) { - if (t->fs == p->fs) - n_fs++; - } - rcu_read_unlock(); - - if (p->fs->users > n_fs) - bprm->unsafe |= LSM_UNSAFE_SHARE; - spin_unlock(&p->fs->lock); } static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file) diff --git a/include/linux/security.h b/include/linux/security.h index 1bc362cb413f..db508a8c3cc7 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -215,9 +215,8 @@ struct sched_param; struct request_sock; /* bprm->unsafe reasons */ -#define LSM_UNSAFE_SHARE 1 -#define LSM_UNSAFE_PTRACE 2 -#define LSM_UNSAFE_NO_NEW_PRIVS 4 +#define LSM_UNSAFE_PTRACE BIT(0) +#define LSM_UNSAFE_NO_NEW_PRIVS BIT(1) #ifdef CONFIG_MMU extern int mmap_min_addr_handler(struct ctl_table *table, int write, diff --git a/security/apparmor/domain.c b/security/apparmor/domain.c index 91689d34d281..1b2c0bb4d9ae 100644 --- a/security/apparmor/domain.c +++ b/security/apparmor/domain.c @@ -924,11 +924,6 @@ int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm) goto audit; } - if (bprm->unsafe & LSM_UNSAFE_SHARE) { - /* FIXME: currently don't mediate shared state */ - ; - } - if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) { /* TODO: test needs to be profile of label to new */ error = may_change_ptraced_domain(new, &info); diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 79573504783b..3ec80cc8ad1c 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -2349,16 +2349,6 @@ static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm) if (rc) return rc; - /* Check for shared state */ - if (bprm->unsafe & LSM_UNSAFE_SHARE) { - rc = avc_has_perm(&selinux_state, - old_tsec->sid, new_tsec->sid, - SECCLASS_PROCESS, PROCESS__SHARE, - NULL); - if (rc) - return -EPERM; - } - /* Make sure that anyone attempting to ptrace over a task that * changes its SID has the appropriate permit */ if (bprm->unsafe & LSM_UNSAFE_PTRACE) {