Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/mseal: update madvise() logic

The madvise() logic is inexplicably performed in mm/mseal.c - this ought
to be located in mm/madvise.c.

Additionally can_modify_vma_madv() is inconsistently named and, in
combination with is_ro_anon(), is very confusing logic.

Put a static function in mm/madvise.c instead - can_madvise_modify() -
that spells out exactly what's happening. Also explicitly check for an
anon VMA.

Also add commentary to explain what's going on.

Essentially - we disallow discarding of data in mseal()'d mappings in
instances where the user couldn't otherwise write to that data.

We retain the existing behaviour here regarding MAP_PRIVATE mappings of
file-backed mappings, which entails some complexity - while this, strictly
speaking - appears to violate mseal() semantics, it may interact badly
with users which expect to be able to madvise(MADV_DONTNEED) .text
mappings for instance.

We may revisit this at a later date.

No functional change intended.

Link: https://lkml.kernel.org/r/492a98d9189646e92c8f23f4cce41ed323fe01df.1753431105.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: Jann Horn <jannh@google.com>
Cc: Jeff Xu <jeffxu@chromium.org>
Cc: Kees Cook <kees@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
d0b47a68 f225b34f

+70 -57
+70 -1
mm/madvise.c
··· 19 19 #include <linux/sched.h> 20 20 #include <linux/sched/mm.h> 21 21 #include <linux/mm_inline.h> 22 + #include <linux/mmu_context.h> 22 23 #include <linux/string.h> 23 24 #include <linux/uio.h> 24 25 #include <linux/ksm.h> ··· 1257 1256 &guard_remove_walk_ops, NULL); 1258 1257 } 1259 1258 1259 + #ifdef CONFIG_64BIT 1260 + /* Does the madvise operation result in discarding of mapped data? */ 1261 + static bool is_discard(int behavior) 1262 + { 1263 + switch (behavior) { 1264 + case MADV_FREE: 1265 + case MADV_DONTNEED: 1266 + case MADV_DONTNEED_LOCKED: 1267 + case MADV_REMOVE: 1268 + case MADV_DONTFORK: 1269 + case MADV_WIPEONFORK: 1270 + case MADV_GUARD_INSTALL: 1271 + return true; 1272 + } 1273 + 1274 + return false; 1275 + } 1276 + 1277 + /* 1278 + * We are restricted from madvise()'ing mseal()'d VMAs only in very particular 1279 + * circumstances - discarding of data from read-only anonymous SEALED mappings. 1280 + * 1281 + * This is because users cannot trivally discard data from these VMAs, and may 1282 + * only do so via an appropriate madvise() call. 1283 + */ 1284 + static bool can_madvise_modify(struct madvise_behavior *madv_behavior) 1285 + { 1286 + struct vm_area_struct *vma = madv_behavior->vma; 1287 + 1288 + /* If the VMA isn't sealed we're good. */ 1289 + if (can_modify_vma(vma)) 1290 + return true; 1291 + 1292 + /* For a sealed VMA, we only care about discard operations. */ 1293 + if (!is_discard(madv_behavior->behavior)) 1294 + return true; 1295 + 1296 + /* 1297 + * We explicitly permit all file-backed mappings, whether MAP_SHARED or 1298 + * MAP_PRIVATE. 1299 + * 1300 + * The latter causes some complications. Because now, one can mmap() 1301 + * read/write a MAP_PRIVATE mapping, write to it, then mprotect() 1302 + * read-only, mseal() and a discard will be permitted. 1303 + * 1304 + * However, in order to avoid issues with potential use of madvise(..., 1305 + * MADV_DONTNEED) of mseal()'d .text mappings we, for the time being, 1306 + * permit this. 1307 + */ 1308 + if (!vma_is_anonymous(vma)) 1309 + return true; 1310 + 1311 + /* If the user could write to the mapping anyway, then this is fine. */ 1312 + if ((vma->vm_flags & VM_WRITE) && 1313 + arch_vma_access_permitted(vma, /* write= */ true, 1314 + /* execute= */ false, /* foreign= */ false)) 1315 + return true; 1316 + 1317 + /* Otherwise, we are not permitted to perform this operation. */ 1318 + return false; 1319 + } 1320 + #else 1321 + static bool can_madvise_modify(struct madvise_behavior *madv_behavior) 1322 + { 1323 + return true; 1324 + } 1325 + #endif 1326 + 1260 1327 /* 1261 1328 * Apply an madvise behavior to a region of a vma. madvise_update_vma 1262 1329 * will handle splitting a vm area into separate areas, each area with its own ··· 1338 1269 struct madvise_behavior_range *range = &madv_behavior->range; 1339 1270 int error; 1340 1271 1341 - if (unlikely(!can_modify_vma_madv(madv_behavior->vma, behavior))) 1272 + if (unlikely(!can_madvise_modify(madv_behavior))) 1342 1273 return -EPERM; 1343 1274 1344 1275 switch (behavior) {
-49
mm/mseal.c
··· 11 11 #include <linux/mman.h> 12 12 #include <linux/mm.h> 13 13 #include <linux/mm_inline.h> 14 - #include <linux/mmu_context.h> 15 14 #include <linux/syscalls.h> 16 15 #include <linux/sched.h> 17 16 #include "internal.h" ··· 18 19 static inline void set_vma_sealed(struct vm_area_struct *vma) 19 20 { 20 21 vm_flags_set(vma, VM_SEALED); 21 - } 22 - 23 - static bool is_madv_discard(int behavior) 24 - { 25 - switch (behavior) { 26 - case MADV_FREE: 27 - case MADV_DONTNEED: 28 - case MADV_DONTNEED_LOCKED: 29 - case MADV_REMOVE: 30 - case MADV_DONTFORK: 31 - case MADV_WIPEONFORK: 32 - case MADV_GUARD_INSTALL: 33 - return true; 34 - } 35 - 36 - return false; 37 - } 38 - 39 - static bool is_ro_anon(struct vm_area_struct *vma) 40 - { 41 - /* check anonymous mapping. */ 42 - if (vma->vm_file || vma->vm_flags & VM_SHARED) 43 - return false; 44 - 45 - /* 46 - * check for non-writable: 47 - * PROT=RO or PKRU is not writeable. 48 - */ 49 - if (!(vma->vm_flags & VM_WRITE) || 50 - !arch_vma_access_permitted(vma, true, false, false)) 51 - return true; 52 - 53 - return false; 54 - } 55 - 56 - /* 57 - * Check if a vma is allowed to be modified by madvise. 58 - */ 59 - bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) 60 - { 61 - if (!is_madv_discard(behavior)) 62 - return true; 63 - 64 - if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma))) 65 - return false; 66 - 67 - /* Allow by default. */ 68 - return true; 69 22 } 70 23 71 24 static int mseal_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
-7
mm/vma.h
··· 577 577 return true; 578 578 } 579 579 580 - bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior); 581 - 582 580 #else 583 581 584 582 static inline bool can_modify_vma(struct vm_area_struct *vma) 585 - { 586 - return true; 587 - } 588 - 589 - static inline bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) 590 583 { 591 584 return true; 592 585 }