From patchwork Mon Jul 27 17:11:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Yznaga X-Patchwork-Id: 11687379 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9947D913 for ; Mon, 27 Jul 2020 18:07:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6549220714 for ; Mon, 27 Jul 2020 18:07:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="l1LOF0dm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6549220714 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id ABA446B0006; Mon, 27 Jul 2020 14:07:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A6BC76B0007; Mon, 27 Jul 2020 14:07:17 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9839F6B0008; Mon, 27 Jul 2020 14:07:17 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0215.hostedemail.com [216.40.44.215]) by kanga.kvack.org (Postfix) with ESMTP id 827106B0006 for ; Mon, 27 Jul 2020 14:07:17 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4302F2AAE for ; Mon, 27 Jul 2020 18:07:17 +0000 (UTC) X-FDA: 77084637714.03.duck31_36034cf26f63 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin03.hostedemail.com (Postfix) with ESMTP id AF534174DC for ; Mon, 27 Jul 2020 18:04:43 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,anthony.yznaga@oracle.com,,RULES_HIT:30054:30064,0,RBL:156.151.31.86:@oracle.com:.lbl8.mailshell.net-64.10.201.10 62.18.0.100;04yrjoqp37o57cjec3kfeiuuywiocop4agaptze5rapzhod91otptwh6htphm3p.979k9eetepm6hcxmo6633p6wt4kbgdppmekrgsenf7mq3w38iiweww3fkmcgb5p.n-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:92,LUA_SUMMARY:none X-HE-Tag: duck31_36034cf26f63 X-Filterd-Recvd-Size: 6139 Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Mon, 27 Jul 2020 18:04:43 +0000 (UTC) Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 06RGgkN5077073; Mon, 27 Jul 2020 17:02:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=EH2bcmGiKig5NQUr25P5VYLKdWqRTYRE1Orwb4KgXik=; b=l1LOF0dmlbi1ncVDq7RWlEvNJYr2LbPMPmGo3aXniYWPZhLlMSHR0iWNp/2cDgmG6dbR RsmBzBxwcJM3BxRVkITqX1WvldXoqwBEQ03KjtD+yDZgD1mbk32LJV5cwSQ381iZh3Y9 BCtLvGVqEyXedSXoHlknOTfyCHrbE6mWURmbGcIEEHGXvrGUEJ5/qtMwH8azy5BTtzxL krFCtP1n2bTqG81bcnd5Xz1b8ajPJlHCouPTuK7bHtdIFfZN519587R0e4/RkkwN7uww /QNbyjqx8Ocr6uD0+RLlm6G5JCez12sO4h0aSAWN1OV1n+iLEpHqky6Wc6yI5u2WSKKf jg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by userp2130.oracle.com with ESMTP id 32hu1j2rvc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 27 Jul 2020 17:02:16 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 06RGgViM055487; Mon, 27 Jul 2020 17:02:16 GMT Received: from pps.reinject (localhost [127.0.0.1]) by userp3020.oracle.com with ESMTP id 32hu5r9fda-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 27 Jul 2020 17:02:16 +0000 Received: from userp3020.oracle.com (userp3020.oracle.com [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 06RGuWGn111604; Mon, 27 Jul 2020 17:02:15 GMT Received: from ca-qasparc-x86-2.us.oracle.com (ca-qasparc-x86-2.us.oracle.com [10.147.24.103]) by userp3020.oracle.com with ESMTP id 32hu5r9f7r-6; Mon, 27 Jul 2020 17:02:15 +0000 From: Anthony Yznaga To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org Cc: mhocko@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, keescook@chromium.org, gerg@linux-m68k.org, ktkhai@virtuozzo.com, christian.brauner@ubuntu.com, peterz@infradead.org, esyr@redhat.com, jgg@ziepe.ca, christian@kellner.me, areber@redhat.com, cyphar@cyphar.com, steven.sistare@oracle.com Subject: [RFC PATCH 5/5] mm: introduce MADV_DOEXEC Date: Mon, 27 Jul 2020 10:11:27 -0700 Message-Id: <1595869887-23307-6-git-send-email-anthony.yznaga@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1595869887-23307-1-git-send-email-anthony.yznaga@oracle.com> References: <1595869887-23307-1-git-send-email-anthony.yznaga@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9695 signatures=668679 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 clxscore=1015 malwarescore=0 spamscore=0 suspectscore=0 bulkscore=0 priorityscore=1501 phishscore=0 mlxlogscore=999 lowpriorityscore=0 impostorscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007270116 X-Rspamd-Queue-Id: AF534174DC X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: madvise MADV_DOEXEC preserves a memory range across exec. Initially only supported for non-executable, non-stack, anonymous memory. MADV_DONTEXEC reverts the effect of a previous MADV_DOXEXEC call and undoes the preservation of the range. After a successful exec call, the behavior of all ranges reverts to MADV_DONTEXEC. Signed-off-by: Steve Sistare Signed-off-by: Anthony Yznaga --- include/uapi/asm-generic/mman-common.h | 3 +++ mm/madvise.c | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index f94f65d429be..7c5f616b28f7 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -72,6 +72,9 @@ #define MADV_COLD 20 /* deactivate these pages */ #define MADV_PAGEOUT 21 /* reclaim these pages */ +#define MADV_DOEXEC 22 /* do inherit across exec */ +#define MADV_DONTEXEC 23 /* don't inherit across exec */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/mm/madvise.c b/mm/madvise.c index dd1d43cf026d..b447fa748649 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -103,6 +103,26 @@ static long madvise_behavior(struct vm_area_struct *vma, case MADV_KEEPONFORK: new_flags &= ~VM_WIPEONFORK; break; + case MADV_DOEXEC: + /* + * MADV_DOEXEC is only supported on private, non-executable, + * non-stack anonymous memory and if the VM_EXEC_KEEP flag + * is available. + */ + if (!VM_EXEC_KEEP || vma->vm_file || vma->vm_flags & (VM_EXEC|VM_SHARED|VM_STACK)) { + error = -EINVAL; + goto out; + } + new_flags |= (new_flags & ~VM_MAYEXEC) | VM_EXEC_KEEP; + break; + case MADV_DONTEXEC: + if (!VM_EXEC_KEEP) { + error = -EINVAL; + goto out; + } + if (new_flags & VM_EXEC_KEEP) + new_flags |= (new_flags & ~VM_EXEC_KEEP) | VM_MAYEXEC; + break; case MADV_DONTDUMP: new_flags |= VM_DONTDUMP; break; @@ -983,6 +1003,8 @@ static int madvise_inject_error(int behavior, case MADV_SOFT_OFFLINE: case MADV_HWPOISON: #endif + case MADV_DOEXEC: + case MADV_DONTEXEC: return true; default: @@ -1037,6 +1059,9 @@ static int madvise_inject_error(int behavior, * MADV_DONTDUMP - the application wants to prevent pages in the given range * from being included in its core dump. * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core dump. + * MADV_DOEXEC - On exec, preserve and duplicate this area in the new process + * if the new process allows it. + * MADV_DONTEXEC - Undo the effect of MADV_DOEXEC. * * return values: * zero - success