From patchwork Wed Sep 25 01:40:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 1989113 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XCzwB5TN1z1xsM for ; Wed, 25 Sep 2024 11:41:14 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1stH1f-00019R-1d; Wed, 25 Sep 2024 01:41:07 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1stH1d-00018p-9c for kernel-team@lists.ubuntu.com; Wed, 25 Sep 2024 01:41:05 +0000 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 192683F335 for ; Wed, 25 Sep 2024 01:41:05 +0000 (UTC) Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2e070439426so225365a91.1 for ; Tue, 24 Sep 2024 18:41:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727228463; x=1727833263; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bX5SDYC90i4S2LMJw4mVHB8qhB9TCpWAyNffFW97EXE=; b=pMvW189S4xmiMTAx6it6xnpyOl1n+A6IQdCXytY7eAhVO3Q6hfgm2Us9+RP6nJ7syz D3KgEe4C+FqK6h1Sp2tn4m1osWLhwLjgoZZ8Lk1ZEBWS0YMoyM//MHkI+L8+1Pq+6vti gIOM7LBem+VNVD2jkkba+QLyrrkv3acT2fVYwwLEi9KbLLwFvKCM9aP6pzSJK4/kM23v +t86fPYHsKy4XQ0IK2qygbEtyXlLcgAr1OV2z4NM9bENZq0aea25Dvlrle0HDozSgMRe 7pmr9hVyLCnUAm2C2O4qfi26u7n9+/nlBpZFSA8/4lBLH+fJZVimwaEEdnnGrT3g2urj Ghsg== X-Gm-Message-State: AOJu0YxoaFIiMT/ozL6gSjCymdP6xH6yybUCVlkB/ot8fDQUdOYkDw7h 9RZUUQ/xLiwYCZ4MOVMUldq2lhhfPotUptmTJc6ffu2pdiSm1Nulijyc0EQ17BnipwnwwqW9T0B 0KL/Wdun3q4n3eH9MyxNX3gPvN1QgHVHC6OJEIqegTXfPdFBfdZLW//pdiEzCHTx7PVHlrlH2s+ 2VeywgeSzhKw== X-Received: by 2002:a17:90a:f596:b0:2c9:7611:e15d with SMTP id 98e67ed59e1d1-2e06ae7752cmr1389498a91.20.1727228463522; Tue, 24 Sep 2024 18:41:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGievaWM7wlJ9VUULgI5RLA1D6fIxQ5O1QttVuSXlrNsrNn8Y8aOMiv931QvuOC+QRBneV6wA== X-Received: by 2002:a17:90a:f596:b0:2c9:7611:e15d with SMTP id 98e67ed59e1d1-2e06ae7752cmr1389482a91.20.1727228463146; Tue, 24 Sep 2024 18:41:03 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:60ab:a189:1ef3:51ef]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e2f93e7sm221132a91.48.2024.09.24.18.41.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2024 18:41:02 -0700 (PDT) From: Koichiro Den To: kernel-team@lists.ubuntu.com Subject: [SRU][N][PATCH 1/1] exec: Fix ToCToU between perm check and set-uid/gid usage Date: Wed, 25 Sep 2024 10:40:29 +0900 Message-ID: <20240925014036.98880-2-koichiro.den@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240925014036.98880-1-koichiro.den@canonical.com> References: <20240925014036.98880-1-koichiro.den@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Kees Cook commit f50733b45d865f91db90919f8311e2127ce5a0cb upstream. When opening a file for exec via do_filp_open(), permission checking is done against the file's metadata at that moment, and on success, a file pointer is passed back. Much later in the execve() code path, the file metadata (specifically mode, uid, and gid) is used to determine if/how to set the uid and gid. However, those values may have changed since the permissions check, meaning the execution may gain unintended privileges. For example, if a file could change permissions from executable and not set-id: ---------x 1 root root 16048 Aug 7 13:16 target to set-id and non-executable: ---S------ 1 root root 16048 Aug 7 13:16 target it is possible to gain root privileges when execution should have been disallowed. While this race condition is rare in real-world scenarios, it has been observed (and proven exploitable) when package managers are updating the setuid bits of installed programs. Such files start with being world-executable but then are adjusted to be group-exec with a set-uid bit. For example, "chmod o-x,u+s target" makes "target" executable only by uid "root" and gid "cdrom", while also becoming setuid-root: -rwxr-xr-x 1 root cdrom 16048 Aug 7 13:16 target becomes: -rwsr-xr-- 1 root cdrom 16048 Aug 7 13:16 target But racing the chmod means users without group "cdrom" membership can get the permission to execute "target" just before the chmod, and when the chmod finishes, the exec reaches brpm_fill_uid(), and performs the setuid to root, violating the expressed authorization of "only cdrom group members can setuid to root". Re-check that we still have execute permissions in case the metadata has changed. It would be better to keep a copy from the perm-check time, but until we can do that refactoring, the least-bad option is to do a full inode_permission() call (under inode lock). It is understood that this is safe against dead-locks, but hardly optimal. Reported-by: Marco Vanotti Tested-by: Marco Vanotti Suggested-by: Linus Torvalds Cc: stable@vger.kernel.org Cc: Eric Biederman Cc: Alexander Viro Cc: Christian Brauner Signed-off-by: Kees Cook Signed-off-by: Greg Kroah-Hartman (cherry picked from commit d2a2a4714d80d09b0f8eb6438ab4224690b7121e linux-6.6.y) CVE-2024-43882 Signed-off-by: Koichiro Den --- fs/exec.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index f11cfd7bce0b..a545245d3ba5 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1661,6 +1661,7 @@ static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file) unsigned int mode; vfsuid_t vfsuid; vfsgid_t vfsgid; + int err; if (!mnt_may_suid(file->f_path.mnt)) return; @@ -1677,12 +1678,17 @@ static void bprm_fill_uid(struct linux_binprm *bprm, struct file *file) /* Be careful if suid/sgid is set */ inode_lock(inode); - /* reload atomically mode/uid/gid now that lock held */ + /* Atomically reload and check mode/uid/gid now that lock held. */ mode = inode->i_mode; vfsuid = i_uid_into_vfsuid(idmap, inode); vfsgid = i_gid_into_vfsgid(idmap, inode); + err = inode_permission(idmap, inode, MAY_EXEC); inode_unlock(inode); + /* Did the exec bit vanish out from under us? Give up. */ + if (err) + return; + /* We ignore suid/sgid if there are no mappings for them in the ns */ if (!vfsuid_has_mapping(bprm->cred->user_ns, vfsuid) || !vfsgid_has_mapping(bprm->cred->user_ns, vfsgid))