From patchwork Wed Sep 25 11:57:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shu Han X-Patchwork-Id: 13811957 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 32CFFC369D3 for ; Wed, 25 Sep 2024 11:57:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2EDE6B0095; Wed, 25 Sep 2024 07:57:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9DE986B0096; Wed, 25 Sep 2024 07:57:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87F9B6B0098; Wed, 25 Sep 2024 07:57:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 69C896B0095 for ; Wed, 25 Sep 2024 07:57:16 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 23A1F80B09 for ; Wed, 25 Sep 2024 11:57:16 +0000 (UTC) X-FDA: 82603110072.16.02F3ED8 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf30.hostedemail.com (Postfix) with ESMTP id 5DC948000A for ; Wed, 25 Sep 2024 11:57:14 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eTt0k9WO; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ebpqwerty472123@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=ebpqwerty472123@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727265301; 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:references:dkim-signature; bh=2kVeDhwyp3qq2JlkrO+2laFA9/t+LTVrg2fHPuiA8mM=; b=vLeXpK3c96wZuGtF+Yu93WyT18tVmLkDU8iPjTvKlqcRMUgjbqAXEyaQ4KmVCrjP57IT7h s4ZYlI72RMrl4wEGly/iWfZ+doju0cP/P66pW8IpEakfPFV+o/aApuNGPhyOnLkXQbnaih 9Woh2Ej4+OoyKB1lcuhxJA5OxL8PEwA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727265301; a=rsa-sha256; cv=none; b=gIkpvvLZOhcoi/euxvRb+QRF4xpIQPICMzKM4WLvQeLY/cyLmlrrmJoDz3YI7L4BffLCsA xQiYSjUiXTJYpS9gG6bPoOPSQ4EymtFG9WMOr//wjGDNnaRaJG2ocXSv/Zp3YzjG8+Uaxo Yjeh5gxEotSNUsUk3WGUcZLbC9NSKq8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eTt0k9WO; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of ebpqwerty472123@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=ebpqwerty472123@gmail.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2054feabfc3so59513025ad.1 for ; Wed, 25 Sep 2024 04:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727265433; x=1727870233; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2kVeDhwyp3qq2JlkrO+2laFA9/t+LTVrg2fHPuiA8mM=; b=eTt0k9WO49FyeCtIyJTuwV2+g7PYmSUIqQrtVmEEQZ3mhos8IE/+b6QyhHEeBcRdYH B9S/svj9832/RtGGmzYRbpskVvoyYceLnBwMsD9ucS4090i4xyMYv97FOR8aOLTUWU4g JrUgKMyDlpk9KmO7S1JiO/6xOVdd8WXEKmUASlgOHUcXs1x0PHmCLNrwUEjFhDSgYnWL EqhlmL46OcReadnJTk3GcL3qQw939Nxh3mZAt4xsFA7GjVFEJF3pbDR/aw8NlwCImrrR fmMTJ3n+wrtktzLDQclEZXOvjGOM30xQFxaExj/Gjw0CLA604GPcAsKJc+LwB2bQf8rv tu3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727265433; x=1727870233; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2kVeDhwyp3qq2JlkrO+2laFA9/t+LTVrg2fHPuiA8mM=; b=kxvatbE3+dqKd0VRHFraCK+O1wUTEjHKAsYeo/LLDMrAzPJFwtXuAED4kAIWeqdNTX lM5LvK90wa4BAfksbdrc+E6FN0RQTaWnZwRb4+3g9uLb9Wcn/y02uLX9Lj7utataBNJc su9V4XsgB+sQiZzUhyITcspgHekoN4jfKMrsIOZQkum9dEIzgRvv+gYwtColnJlik05R Y0kjT3c/qtymWjrcck/X9T7T1Ulnam8whYEOcGFzawLT1Ac6je32hsTm/QSnqwI7fHRT mn7KTtE6Nb2vvW9i7niBUFTV2EXJKJituMC7HPVbuni0f3Rk/+5EB3GeBCS3WUS6ZHkB 1fsw== X-Gm-Message-State: AOJu0Yyy/bFOw1UWhCv5K17X53UoFTZB820Nm54d4z7YxeMMXtPMve+R 6AFtQbuN20nWM36nUU2w1FiSLfZkbETHyS6o1y7uVPrXHEwW3zG1ZgNhLQ== X-Google-Smtp-Source: AGHT+IEk8pPYrms9s27HbZBRf9WGxJI4CcOZxynSGdGZzlqeTJ4SAtNVpNTKec9qDw+CqpSLwR/QuQ== X-Received: by 2002:a17:903:24c:b0:206:9693:7d5b with SMTP id d9443c01a7336-20afc641ad4mr32952595ad.55.1727265432830; Wed, 25 Sep 2024 04:57:12 -0700 (PDT) Received: from localhost.localdomain ([20.37.103.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20af17e2dc9sm23565585ad.135.2024.09.25.04.57.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2024 04:57:12 -0700 (PDT) From: Shu Han To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH RFC v2] mm: move security_mmap_file() back into do_mmap() Date: Wed, 25 Sep 2024 19:57:01 +0800 Message-Id: <20240925115701.73-1-ebpqwerty472123@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5DC948000A X-Stat-Signature: 7wrg5gsz9r5at9x4i4qnybcati7jbj69 X-Rspam-User: X-HE-Tag: 1727265434-220006 X-HE-Meta: U2FsdGVkX19dcK+Bg4ezmYVc/YXi/LSX8fjLbrsTvE85wnoAjwkXQGWQ6lf43N8zL0/msRF7nsPXQYXk+pEMJtpZ6jDWtgbQeTo37auGjUS8Y9jmyHWf1qw+WMD5WzxFdRdaHx25yJ4lYRqe0pBuU/F4ROqnb6ZF8CL30TYLThR9jIFDCKQFpSec2+1TapxiUqti3tR6RFFE6QnDs022eWbuR4ZaCMWIehbmXYba4BLnQH5xk0mOitAAKNLTfStIr1Ejf1ylST9pYcaKimAxvbyQskh7OqaarMFb3grfsIu1VJiX8E0POvbQM8S5Uj7VHost/0ezHtmWR2JKoQj81LoMijT62k8b9JKBsgrcHdtmuBcMnrdr2NE6ZqWm5b41epsW4CKrFvYrXoqcr4ZGMq37XDZvgf2Q4y1puXzO+syPZ+1WDt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fKK2s+gt4bY8scIvPNTccJCULSC54dPOnbdBNrAbs= 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: List-Subscribe: List-Unsubscribe: This patch moves the security_mmap_file() back into do_mmap(), which revert(It is conceptually a revert of the commit(revert the feature change in the commit), but not in the git sense(revert lines)) the commit 8b3ec6814c83d76b85bd13badc48552836c24839 ("take security_mmap_file() outside of ->mmap_sem"). Below is the reason. Some logic may call do_mmap() without calling security_mmap_file(), without being aware of the harm this poses to LSM. For example, CVE-2016-10044[1] was reported many years ago, but the remap_file_pages() can still bypass the W^X policy enforced by SELinux[2] for a long time. Adding checks is easy(and has been done in all required call sites for the current version), but there may have more calls to do_mmap() in the future. Moving security_mmap_file() back into do_mmap() can avoid forgetting, and avoid repeated logic for whether READ_IMPLIES_EXEC should add PROT_EXEC for the mapping or not(In current, the !MMU case won't imply exec if the file's mmap_capabilities is not exist, but the security check logic is different). It is noteworthy that moving the security check in do_mmap() will let it in the mmap_write_lock, which slows down the performance and even have deadlocks if someone depends on it(Since security_file_mprotect() is already in the lock, this possibility is tiny), which requires LSM modules to check. Link: https://project-zero.issues.chromium.org/issues/42452389 [1] Link: https://lore.kernel.org/all/20240919080905.4506-2-paul@paul-moore.com/ [2] Signed-off-by: Shu Han --- V2: Add RFC tag as lorenzo.stoakes@oracle.com suggested, and refine the comment in patch. V1: https://lore.kernel.org/all/20240925081628.408-1-ebpqwerty472123@gmail.com/ Alternatives: 1. mm: move the check of READ_IMPLIES_EXEC out of do_mmap() 2. Add sufficient comments for do_mmap() --- include/linux/security.h | 8 ++++---- ipc/shm.c | 4 ---- mm/mmap.c | 9 +++++---- mm/nommu.c | 5 ++++- mm/util.c | 19 ++++++++----------- security/security.c | 41 ++++------------------------------------ 6 files changed, 25 insertions(+), 61 deletions(-) base-commit: f89722faa31466ff41aed21bdeb9cf34c2312858 diff --git a/include/linux/security.h b/include/linux/security.h index c37c32ebbdcd..e061bc9a0331 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -423,8 +423,8 @@ void security_file_free(struct file *file); int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg); int security_file_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg); -int security_mmap_file(struct file *file, unsigned long prot, - unsigned long flags); +int security_mmap_file(struct file *file, unsigned long reqprot, + unsigned long prot, unsigned long flags); int security_mmap_addr(unsigned long addr); int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot); @@ -1077,8 +1077,8 @@ static inline int security_file_ioctl_compat(struct file *file, return 0; } -static inline int security_mmap_file(struct file *file, unsigned long prot, - unsigned long flags) +static inline int security_mmap_file(struct file *file, unsigned long reqprot, + unsigned long prot, unsigned long flags) { return 0; } diff --git a/ipc/shm.c b/ipc/shm.c index 3e3071252dac..ce02560b856f 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -1636,10 +1636,6 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, sfd->vm_ops = NULL; file->private_data = sfd; - err = security_mmap_file(file, prot, flags); - if (err) - goto out_fput; - if (mmap_write_lock_killable(current->mm)) { err = -EINTR; goto out_fput; diff --git a/mm/mmap.c b/mm/mmap.c index 18fddcce03b8..56f9520f85ab 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1260,6 +1260,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, { struct mm_struct *mm = current->mm; int pkey = 0; + unsigned long reqprot = prot, err; *populate = 0; @@ -1276,6 +1277,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!(file && path_noexec(&file->f_path))) prot |= PROT_EXEC; + err = security_mmap_file(file, reqprot, prot, flags); + if (err) + return err; + /* force arch specific MAP_FIXED handling in get_unmapped_area */ if (flags & MAP_FIXED_NOREPLACE) flags |= MAP_FIXED; @@ -3198,12 +3203,8 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, flags |= MAP_LOCKED; file = get_file(vma->vm_file); - ret = security_mmap_file(vma->vm_file, prot, flags); - if (ret) - goto out_fput; ret = do_mmap(vma->vm_file, start, size, prot, flags, 0, pgoff, &populate, NULL); -out_fput: fput(file); out: mmap_write_unlock(mm); diff --git a/mm/nommu.c b/mm/nommu.c index 7296e775e04e..e632f3105a5a 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -681,7 +681,7 @@ static int validate_mmap_request(struct file *file, unsigned long pgoff, unsigned long *_capabilities) { - unsigned long capabilities, rlen; + unsigned long capabilities, rlen, reqprot = prot; int ret; /* do the simple checks first */ @@ -818,6 +818,9 @@ static int validate_mmap_request(struct file *file, } /* allow the security API to have its say */ + ret = security_mmap_file(file, reqprot, prot, flags); + if (ret < 0) + return ret; ret = security_mmap_addr(addr); if (ret < 0) return ret; diff --git a/mm/util.c b/mm/util.c index bd283e2132e0..47345e927a8f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -581,17 +581,14 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long populate; LIST_HEAD(uf); - ret = security_mmap_file(file, prot, flag); - if (!ret) { - if (mmap_write_lock_killable(mm)) - return -EINTR; - ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate, - &uf); - mmap_write_unlock(mm); - userfaultfd_unmap_complete(mm, &uf); - if (populate) - mm_populate(ret, populate); - } + if (mmap_write_lock_killable(mm)) + return -EINTR; + ret = do_mmap(file, addr, len, prot, flag, 0, pgoff, &populate, + &uf); + mmap_write_unlock(mm); + userfaultfd_unmap_complete(mm, &uf); + if (populate) + mm_populate(ret, populate); return ret; } diff --git a/security/security.c b/security/security.c index 4564a0a1e4ef..25556629f588 100644 --- a/security/security.c +++ b/security/security.c @@ -2927,42 +2927,10 @@ int security_file_ioctl_compat(struct file *file, unsigned int cmd, } EXPORT_SYMBOL_GPL(security_file_ioctl_compat); -static inline unsigned long mmap_prot(struct file *file, unsigned long prot) -{ - /* - * Does we have PROT_READ and does the application expect - * it to imply PROT_EXEC? If not, nothing to talk about... - */ - if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ) - return prot; - if (!(current->personality & READ_IMPLIES_EXEC)) - return prot; - /* - * if that's an anonymous mapping, let it. - */ - if (!file) - return prot | PROT_EXEC; - /* - * ditto if it's not on noexec mount, except that on !MMU we need - * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case - */ - if (!path_noexec(&file->f_path)) { -#ifndef CONFIG_MMU - if (file->f_op->mmap_capabilities) { - unsigned caps = file->f_op->mmap_capabilities(file); - if (!(caps & NOMMU_MAP_EXEC)) - return prot; - } -#endif - return prot | PROT_EXEC; - } - /* anything on noexec mount won't get PROT_EXEC */ - return prot; -} - /** * security_mmap_file() - Check if mmap'ing a file is allowed * @file: file + * @reqprot: protection requested by user * @prot: protection applied by the kernel * @flags: flags * @@ -2971,11 +2939,10 @@ static inline unsigned long mmap_prot(struct file *file, unsigned long prot) * * Return: Returns 0 if permission is granted. */ -int security_mmap_file(struct file *file, unsigned long prot, - unsigned long flags) +int security_mmap_file(struct file *file, unsigned long reqprot, + unsigned long prot, unsigned long flags) { - return call_int_hook(mmap_file, file, prot, mmap_prot(file, prot), - flags); + return call_int_hook(mmap_file, file, reqprot, prot, flags); } /**