Skip to content

Commit 4693c5d

Browse files
Steve Sistarejfvogel
authored andcommitted
mm: introduce MADV_DOEXEC
madvise MADV_DOEXEC preserves a memory range across exec. Initially only supported for non-executable, non-stack, anonymous memory. MADV_DOEXEC is single-use and after exec madvise must done again to preserve memory for a subsequent exec. MADV_DONTEXEC reverts the effect of a previous MADV_DOXEXEC call and undoes the preservation of the range. Orabug: 32387875 Signed-off-by: Steve Sistare <[email protected]> Signed-off-by: Anthony Yznaga <[email protected]> Reviewed-by: Liam R. Howlett <[email protected]>
1 parent d1a6a64 commit 4693c5d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/uapi/asm-generic/mman-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
#define MADV_POPULATE_READ 22 /* populate (prefault) page tables readable */
7777
#define MADV_POPULATE_WRITE 23 /* populate (prefault) page tables writable */
7878

79+
#define MADV_DOEXEC 24 /* do inherit across exec */
80+
#define MADV_DONTEXEC 25 /* don't inherit across exec */
81+
7982
/* compatibility flags */
8083
#define MAP_FILE 0
8184

mm/madvise.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ static long madvise_behavior(struct vm_area_struct *vma,
106106
case MADV_KEEPONFORK:
107107
new_flags &= ~VM_WIPEONFORK;
108108
break;
109+
case MADV_DOEXEC:
110+
/*
111+
* MADV_DOEXEC is only supported on private, non-executable,
112+
* non-stack anonymous memory and if the VM_EXEC_KEEP flag
113+
* is available.
114+
*/
115+
if (!VM_EXEC_KEEP || !vma_is_anonymous(vma) || vma->vm_flags & (VM_EXEC|VM_SHARED|VM_STACK)) {
116+
error = -EINVAL;
117+
goto out;
118+
}
119+
new_flags |= (new_flags & ~VM_MAYEXEC) | VM_EXEC_KEEP;
120+
break;
121+
case MADV_DONTEXEC:
122+
if (!VM_EXEC_KEEP) {
123+
error = -EINVAL;
124+
goto out;
125+
}
126+
if (new_flags & VM_EXEC_KEEP)
127+
new_flags |= (new_flags & ~VM_EXEC_KEEP) | VM_MAYEXEC;
128+
break;
109129
case MADV_DONTDUMP:
110130
new_flags |= VM_DONTDUMP;
111131
break;
@@ -1035,6 +1055,8 @@ madvise_behavior_valid(int behavior)
10351055
case MADV_SOFT_OFFLINE:
10361056
case MADV_HWPOISON:
10371057
#endif
1058+
case MADV_DOEXEC:
1059+
case MADV_DONTEXEC:
10381060
return true;
10391061

10401062
default:
@@ -1111,6 +1133,9 @@ process_madvise_behavior_valid(int behavior)
11111133
* triggering read faults if required
11121134
* MADV_POPULATE_WRITE - populate (prefault) page tables writable by
11131135
* triggering write faults if required
1136+
* MADV_DOEXEC - On exec, preserve and duplicate this area in the new process
1137+
* if the new process allows it.
1138+
* MADV_DONTEXEC - Undo the effect of MADV_DOEXEC.
11141139
*
11151140
* return values:
11161141
* zero - success

0 commit comments

Comments
 (0)