From patchwork Wed Sep 25 06:30:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shu Han X-Patchwork-Id: 13811635 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 D1A58CF9C6C for ; Wed, 25 Sep 2024 06:30:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39E886B007B; Wed, 25 Sep 2024 02:30:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34E886B0082; Wed, 25 Sep 2024 02:30:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EF1B6B0083; Wed, 25 Sep 2024 02:30:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F2A9B6B007B for ; Wed, 25 Sep 2024 02:30:53 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9E20C808A8 for ; Wed, 25 Sep 2024 06:30:53 +0000 (UTC) X-FDA: 82602287586.21.E413739 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf26.hostedemail.com (Postfix) with ESMTP id D7851140009 for ; Wed, 25 Sep 2024 06:30:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Mm9w93aF; spf=pass (imf26.hostedemail.com: domain of ebpqwerty472123@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=ebpqwerty472123@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727245790; a=rsa-sha256; cv=none; b=xMrzoVgOg+s7xqSp1sQwYQycK6OHAAVy2AJqvaZK+QtZqnmH+sGkARYZZEa9eUJ3SfNmiW l3XXGzs9Uuv6w9irhJ19DOvMrspSILLEVu2hIOm69UuHReqJfqJ6UJLs6bHg7+stslzh2z mIbGjiTzMfEzbvgUA80YatJ6tPSPWFc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Mm9w93aF; spf=pass (imf26.hostedemail.com: domain of ebpqwerty472123@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=ebpqwerty472123@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727245790; 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=TEgjym0TtI38Qrfj1UEKJYqreTTTMFphxHtKrT+D23c=; b=P3GJ+jIEonCq7H26Y4+fWhmNysbQjwUsBcsW8Bn3dtZ681qlmf30bGPnc/a5hbERFuSH3p UMOdpkrEP+t2uEyQpeaNzbSvgo6GkNpYnbGHKKS8QNDvPORzsyxF2uX+zIISoXfNI6XbeX s+b5+snbiJwPVVn4bvBsQcp1cW90kLg= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7d50a42ce97so2706770a12.0 for ; Tue, 24 Sep 2024 23:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727245850; x=1727850650; 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=TEgjym0TtI38Qrfj1UEKJYqreTTTMFphxHtKrT+D23c=; b=Mm9w93aFsoCF6p4B7aJPZhPSMBZ35A0DXfdLmN6DXanhmSwgHE54HzHVqsa23ery+5 11134IlztgDTHvkRi8aVMLp99nK+RKSek6Q9o+MPZHldjoMS4H0+RCHPJcuinONfnJ3W oZtkJX72Ou2iIjHd2BfVVEo6Ww0zr1YHZkv63sNPSv6hEZbVfTxbaZ2wSDNAiZK02yhJ 6DY/0Yq/Cwll0S0jlAyS4Kj7TV4Mq2RzTqPpKLfkuYQepYXt7lwVGK7ncabceUXFe440 umuO3cQkLT6/f++xhY0jIMwXQhHyFUx/bwXgQMYzRMEPDWtOQLNFATdLv6ZhQ/tpNzb+ vpfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727245850; x=1727850650; 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=TEgjym0TtI38Qrfj1UEKJYqreTTTMFphxHtKrT+D23c=; b=U0k0acZVaAx/T/bUsNpmrDYF//mGREbNdDKPqAWOs+e81zthi83cox1g4wcyPu+oT2 h3UNX3/uwjkBCODV5ur59uFjz/x/8jdgd6pHSh1GQkwtlsTkyGck2K8MEenZVhNOfEmU +f9e806Hd/WlE49QTEM4TdgFb3M/93j+0SfvmXw6ffw0s8C5UbO89vFpTJxMDOFzwfdM XlchSpttRKKsmyYwpCqcz+Yy7Fc5l/6TK1DNE0nA7rCnQXcwSiyAFwZ9s0Pg4H2z6A5h NOI4WW4YKTU9ds0/DOGVkwYqJYcd18sMwRWi56Whx/Z+st1LGI6SlA2ARDmPT22asyy7 z5UA== X-Gm-Message-State: AOJu0YyNjGqc9gSz5BixaCCs3ADnFSE/m7fNjgYD/J2OptcswsOTz1Ml xb/f9pC8PgDCGGWzA27Mfus7/A8Jz3kWzRCZ5sNF0QlIRYOoAcuD X-Google-Smtp-Source: AGHT+IGkXk5G726B1bvlVw1YnfU7Z8q/7HoDlL0ZukpUfuuT9sk6ReokRRqVgJQPh6wRbW7t/yMP7Q== X-Received: by 2002:a17:90a:e41:b0:2d8:b43b:6ecc with SMTP id 98e67ed59e1d1-2e06ae27935mr2301917a91.4.1727245850255; Tue, 24 Sep 2024 23:30:50 -0700 (PDT) Received: from localhost.localdomain ([20.37.103.148]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1e5ac1sm692402a91.34.2024.09.24.23.30.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Sep 2024 23:30:49 -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] mm: move the check of READ_IMPLIES_EXEC out of do_mmap() Date: Wed, 25 Sep 2024 14:30:34 +0800 Message-Id: <20240925063034.169-1-ebpqwerty472123@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Stat-Signature: e9u9qkpennaticmgcw311yz1rh7w87us X-Rspamd-Queue-Id: D7851140009 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1727245851-544718 X-HE-Meta: U2FsdGVkX183dYIIPWqIN8db+rw2gRui4/yOAbQr6dNjLt7Guhegj8NUc5JdhpurK6QERqpxtx7G3KscEU/525UYDxWlxHfkJzgDNYgN7eb9VytXyUirD3HH0C8RI/AWb03Pu6ciGtBbo3P3jen88OPtAFq0B66sBJ83Dkc3CMFrGwrw2v5FKmyHltnc3sJS2PsQ5+4NSM0c+7UVBlNGKPhKGTmF5S8RaXj66wzv4TEfmQFtW2SCRHTYNwynMwlbC1MymsPjkIRvZaEC/DlNAo6a4wzPxOzi/WM74ho9wGtMSMO7N2L+LODpm7RktF0BjLZ5WFj/U0IUbZukroCwxlqcVQNxVIuNzT58kLBvZ84/JELMqjeFboGWkqN8LKi0HHO0Nz+3iZdUQBUyImnUyNvquMMDXbivGhOc5dpPZf6hmx6sGmd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jTmizhxXcOTUeBvgtZLaJqQ== 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 check of READ_IMPLIES_EXEC out of do_mmap(), and calls the LSM hooks at the same time. The below is reason. Some logic may want to call do_mmap(), but they are not aware of the harm this poses to LSM. For example, CVE-2016-10044[1] has reported many years ago, but the remap_file_pages() can still bypass W^X policy enforced by SELinux[2]. Add a check is easy, but there may have more calls to do_mmap() in the furture. And move the security check in do_mmap() will let it in the mmap_write_lock, which slow down the performance and even have deadlocks if someone depends on it. Moving the check of READ_IMPLIES_EXEC out of do_mmap() can keep the LSM hook is called out of the mmap_write_lock, 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 implies exec if file's mmap_capabilities is not exist, but the security check logic is different). And all known cases that call do_mmap() become secure and anyone want to apply the READ_IMPLIES_EXEC logic will call the LSM hook correctly. Current call sites to do_mmap(): arch/x86/kernel/shstk.c: must not want to PROT_EXEC in any case. fs/aio.c: have ensured never have PROT_EXEC in the fix of CVE-2016-10044. mm/util.c and ipc/shm.c: the common case mm/mmap.c: in the history, remap_file_pages won't care about the READ_IMPLIES_EXEC. this side effect is introduced in the emulation version, after the deprecated mark exist. 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 --- An alternative method is moving security_file_mmap() back into do_mmap(), i.e. revert the commit 8b3ec6814c83d76b85bd13badc48552836c24839 ("take security_mmap_file() outside of ->mmap_sem"), which is simpler, but it will affect performance. --- include/linux/mm.h | 2 ++ include/linux/security.h | 8 +++---- ipc/shm.c | 2 +- mm/mmap.c | 42 ++++++++++++++++++++++------------ mm/nommu.c | 49 +++++++++++++++++++++++++++++++--------- mm/util.c | 2 +- security/security.c | 41 ++++----------------------------- 7 files changed, 78 insertions(+), 68 deletions(-) base-commit: f89722faa31466ff41aed21bdeb9cf34c2312858 diff --git a/include/linux/mm.h b/include/linux/mm.h index c4b238a20b76..83f334590b06 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3392,6 +3392,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, extern unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf); +extern int mmap_check_prot(struct file *file, unsigned long *prot, + unsigned long flags); extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate, 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..f1095ee3796d 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -1636,7 +1636,7 @@ 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); + err = mmap_check_prot(file, &prot, flags); if (err) goto out_fput; diff --git a/mm/mmap.c b/mm/mmap.c index 18fddcce03b8..b8992ee202fe 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1249,8 +1249,36 @@ static inline bool file_mmap_ok(struct file *file, struct inode *inode, return true; } +int mmap_check_prot(struct file *file, unsigned long *prot, + unsigned long flags) +{ + unsigned long req_prot = *prot; + unsigned long new_prot = req_prot; + int err; + + /* + * Does the application expect PROT_READ to imply PROT_EXEC? + * + * (the exception is when the underlying filesystem is noexec + * mounted, in which case we don't add PROT_EXEC.) + */ + if (((req_prot & (PROT_READ | PROT_EXEC)) == PROT_READ) && + (current->personality & READ_IMPLIES_EXEC) && + !(file && path_noexec(&file->f_path))) + new_prot |= PROT_EXEC; + + err = security_mmap_file(file, req_prot, new_prot, flags); + if (err) + return err; + + *prot = new_prot; + return 0; +} + /* * The caller must write-lock current->mm->mmap_lock. + * The caller must ensure security check is not needed or + * call to mmap_check_prot before. */ unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, @@ -1266,16 +1294,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!len) return -EINVAL; - /* - * Does the application expect PROT_READ to imply PROT_EXEC? - * - * (the exception is when the underlying filesystem is noexec - * mounted, in which case we don't add PROT_EXEC.) - */ - if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) - if (!(file && path_noexec(&file->f_path))) - prot |= PROT_EXEC; - /* force arch specific MAP_FIXED handling in get_unmapped_area */ if (flags & MAP_FIXED_NOREPLACE) flags |= MAP_FIXED; @@ -3198,12 +3216,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..96761add1295 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -792,12 +792,6 @@ static int validate_mmap_request(struct file *file, if (path_noexec(&file->f_path)) { if (prot & PROT_EXEC) return -EPERM; - } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) { - /* handle implication of PROT_EXEC by PROT_READ */ - if (current->personality & READ_IMPLIES_EXEC) { - if (capabilities & NOMMU_MAP_EXEC) - prot |= PROT_EXEC; - } } else if ((prot & PROT_READ) && (prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC) @@ -810,11 +804,6 @@ static int validate_mmap_request(struct file *file, * privately mapped */ capabilities = NOMMU_MAP_COPY; - - /* handle PROT_EXEC implication by PROT_READ */ - if ((prot & PROT_READ) && - (current->personality & READ_IMPLIES_EXEC)) - prot |= PROT_EXEC; } /* allow the security API to have its say */ @@ -992,6 +981,44 @@ static int do_mmap_private(struct vm_area_struct *vma, return -ENOMEM; } +int mmap_check_prot(struct file *file, unsigned long *prot, + unsigned long flags) +{ + unsigned long req_prot = *prot; + unsigned long new_prot = req_prot; + int err; + + /* + * Does the application expect PROT_READ to imply PROT_EXEC? + * + * (the exception is when the underlying filesystem is noexec + * mounted or the file does not have NOMMU_MAP_EXEC + * (== VM_MAYEXEC), in which case we don't add PROT_EXEC.) + */ + if ((req_prot & (PROT_READ | PROT_EXEC)) != PROT_READ) + goto check; + if (!(current->personality & READ_IMPLIES_EXEC)) + goto check; + if (!file) { + new_prot |= PROT_EXEC; + goto check; + } + if (file->f_op->mmap_capabilities) { + unsigned int caps = file->f_op->mmap_capabilities(file); + + if (!(caps & NOMMU_MAP_EXEC)) + goto check; + new_prot |= PROT_EXEC; + } +check: + err = security_mmap_file(file, req_prot, new_prot, flags); + if (err) + return err; + + *prot = new_prot; + return 0; +} + /* * handle mapping creation for uClinux */ diff --git a/mm/util.c b/mm/util.c index bd283e2132e0..2eb4d6037610 100644 --- a/mm/util.c +++ b/mm/util.c @@ -581,7 +581,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long populate; LIST_HEAD(uf); - ret = security_mmap_file(file, prot, flag); + ret = mmap_check_prot(file, &prot, flag); if (!ret) { if (mmap_write_lock_killable(mm)) return -EINTR; 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); } /**