Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 0520A19F487 for ; Tue, 22 Oct 2024 18:45:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729622732; cv=none; b=KEp36rqPgHcL1zKxdkZcHMh0YwHNLNJptE1A6qzlDr+iw4nPT4nFY6Xysxgk7T7idf7RU8LicEMg6w3IeAZX+AEfjbg7a1v835FZKD3fXrV2o8r33jOxIiJlx73+fJRN4mHfMvcFucMDTRLNCaK1LGsrC4ZCrF9xkV/sRMCoZNU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729622732; c=relaxed/simple; bh=Ts68eKQ0k2SX2rKkD4ujE9T0uytM6SNn7hUUGZybJmk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=oXqE25BfBoAieBOD5ZkwDTzy2m0Tg6TKaocxb5p2eTJh7XNUKDeGglS2H/aUvn3jE4VbNtyn7/cQqRrneop19FWok5z4yTXITxar8cGOTiBURiR7LDlAfIe9d4/J6Kbnepl4XwsF+QejELh9jXLgxoMLR7BrrLvYxrWLj2spzTg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=K4bYYqzz; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="K4bYYqzz" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=bHp2aMVL5wG4h4ETfXBjbhHWCYZ7ca5jDeDqVuS7euM=; b=K4bYYqzzeh+Mv4/jxIb3GH+VLj Pyxt8KCC0fVQebrplv7rAJ9S9XCjMzpFwLCwqp7oORjEcJSCyij7AxQkQh0evWJ8qp2TJBnbv2BIM 9hBp2RbOhckgZCwZIYNPWW6x6EkUrZLP/rnLGIv/jpKh7suW6KEPVfq73ss7S8Pm8vikpewp4SaW4 9IcA09rpWkELX0Yeh8gVhFAXlmQQwj1un5gTLZbvqjKDsUuq42QRc7eEpylMypRH1WehpN3ZH61Xn oIWa4fxyNEi3hjgmivbgPd/1nawJqhmk/MyJJlK5sJUVONVPsG8YtMkzgG1aGx1Yq+Q+GCjZESySV zVfZJsUA==; Received: from 179-125-79-219-dinamico.pombonet.net.br ([179.125.79.219] helo=localhost.localdomain) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t3Jsc-00DiXz-QG; Tue, 22 Oct 2024 20:45:19 +0200 From: Thadeu Lima de Souza Cascardo To: stable@vger.kernel.org Cc: Mateusz Guzik , Christian Brauner , Kees Cook , Thadeu Lima de Souza Cascardo Subject: [PATCH 6.6] exec: don't WARN for racy path_noexec check Date: Tue, 22 Oct 2024 15:44:58 -0300 Message-Id: <20241022184458.3601255-1-cascardo@igalia.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Mateusz Guzik [ Upstream commit 0d196e7589cefe207d5d41f37a0a28a1fdeeb7c6 ] Both i_mode and noexec checks wrapped in WARN_ON stem from an artifact of the previous implementation. They used to legitimately check for the condition, but that got moved up in two commits: 633fb6ac3980 ("exec: move S_ISREG() check earlier") 0fd338b2d2cd ("exec: move path_noexec() check earlier") Instead of being removed said checks are WARN_ON'ed instead, which has some debug value. However, the spurious path_noexec check is racy, resulting in unwarranted warnings should someone race with setting the noexec flag. One can note there is more to perm-checking whether execve is allowed and none of the conditions are guaranteed to still hold after they were tested for. Additionally this does not validate whether the code path did any perm checking to begin with -- it will pass if the inode happens to be regular. Keep the redundant path_noexec() check even though it's mindless nonsense checking for guarantee that isn't given so drop the WARN. Reword the commentary and do small tidy ups while here. Signed-off-by: Mateusz Guzik Link: https://lore.kernel.org/r/20240805131721.765484-1-mjguzik@gmail.com [brauner: keep redundant path_noexec() check] Signed-off-by: Christian Brauner [cascardo: keep exit label and use it] Signed-off-by: Thadeu Lima de Souza Cascardo --- fs/exec.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index f49b352a6032..7776209d98c1 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -143,13 +143,11 @@ SYSCALL_DEFINE1(uselib, const char __user *, library) goto out; /* - * may_open() has already checked for this, so it should be - * impossible to trip now. But we need to be extra cautious - * and check again at the very end too. + * Check do_open_execat() for an explanation. */ error = -EACCES; - if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) || - path_noexec(&file->f_path))) + if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) || + path_noexec(&file->f_path)) goto exit; error = -ENOEXEC; @@ -925,23 +923,22 @@ static struct file *do_open_execat(int fd, struct filename *name, int flags) file = do_filp_open(fd, name, &open_exec_flags); if (IS_ERR(file)) - goto out; + return file; /* - * may_open() has already checked for this, so it should be - * impossible to trip now. But we need to be extra cautious - * and check again at the very end too. + * In the past the regular type check was here. It moved to may_open() in + * 633fb6ac3980 ("exec: move S_ISREG() check earlier"). Since then it is + * an invariant that all non-regular files error out before we get here. */ err = -EACCES; - if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode) || - path_noexec(&file->f_path))) + if (WARN_ON_ONCE(!S_ISREG(file_inode(file)->i_mode)) || + path_noexec(&file->f_path)) goto exit; err = deny_write_access(file); if (err) goto exit; -out: return file; exit: -- 2.34.1