mm, mmap: fix vma_merge() case 7 with vma_ops->close
Subsystems (3)
PATCH v2 1/1 mm, mmap: fix vma_merge() case 7 with vma_ops->close
When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of mprotect() operations can result in an elevated shm_nattch and thus leak of the resource.
The problem is caused by wrong assumptions in vma_merge() commit 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in mergeability test"). The shmem vmas have a vma_ops->close callback that decrements shm_nattch, and we remove the vma without calling it.
vma_merge() has thus historically avoided merging vma's with vma_ops->close and commit 714965ca8252 was supposed to keep it that way. It relaxed the checks for vma_ops->close in can_vma_merge_after() assuming that it is never called on a vma that would be a candidate for removal. However, the vma_merge() code does also use the result of this check in the decision to remove a different vma in the merge case 7.
A robust solution would be to refactor vma_merge() code in a way that the vma_ops->close check is only done for vma's that are actually going to be removed, and not as part of the preliminary checks. That would both solve the existing bug, and also allow additional merges that the checks currently prevent unnecessarily in some cases.
However to fix the existing bug first with a minimized risk, and for easier stable backports, this patch only adds a vma_ops->close check to the buggy case 7 specifically. All other cases of vma removal are covered by the can_vma_merge_before() check that includes the test for vma_ops->close.
The reproducer code, adapted from Michal Hocko's code:
int main(int argc, char *argv[]) {
int segment_id;
size_t segment_size = 20 * PAGE_SIZE;
char * sh_mem;
struct shmid_ds shmid_ds;
key_t key = 0x1234;
segment_id = shmget(key, segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
sh_mem = (char *)shmat(segment_id, NULL, 0);
mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_NONE);
mprotect(sh_mem + PAGE_SIZE, PAGE_SIZE, PROT_WRITE);
mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_WRITE);
shmdt(sh_mem);
shmctl(segment_id, IPC_STAT, &shmid_ds);
printf("nattch after shmdt(): %lu (expected: 0)\n", shmid_ds.shm_nattch); if (shmctl(segment_id, IPC_RMID, 0))
printf("IPCRM failed %d\n", errno); return (shmid_ds.shm_nattch) ? 1 : 0;
}
---
v2: deduplicate code, per Lorenzo
mm/mmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
mm/mmap.c +9 -1
@@ -954,13 +954,21 @@ static struct vm_area_struct }else if (merge_prev) {/* case 2 */ if (curr) {vma_start_write (curr); - err =dup_anon_vma (prev, curr, &anon_dup);if (end == curr->vm_end) {/* case 7 */ +/* +* can_vma_merge_after() assumed we would not be +* removing prev vma, so it skipped the check +* for vm_ops->close, but we are removing curr +*/ +if (curr->vm_ops && curr->vm_ops->close) + err = -EINVAL ; remove = curr; }else {/* case 5 */ adjust = curr; adj_start = (end - curr->vm_start); } +if (!err) + err =dup_anon_vma (prev, curr, &anon_dup); } }else {/* merge_next */ vma_start_write (next);
Signature · 1 line
2.43.1
Quoted 97 lines · Vlastimil Babka
When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of
mprotect()operations can result in an elevatedshm_nattchand thus leak of the resource.The problem is caused by wrong assumptions in
vma_merge()commit714965ca8252("mm/mmap: start distinguishing if vma can be removed in mergeability test"). The shmem vmas have avma_ops->closecallback that decrementsshm_nattch, and we remove the vma without calling it.
vma_merge()has thus historically avoided merging vma's withvma_ops->closeand commit714965ca8252was supposed to keep it that way. It relaxed the checks forvma_ops->closeincan_vma_merge_after()assuming that it is never called on a vma that would be a candidate for removal. However, thevma_merge()code does also use the result of this check in the decision to remove a different vma in the merge case 7.A robust solution would be to refactor
vma_merge()code in a way that thevma_ops->closecheck is only done for vma's that are actually going to be removed, and not as part of the preliminary checks. That would both solve the existing bug, and also allow additional merges that the checks currently prevent unnecessarily in some cases.However to fix the existing bug first with a minimized risk, and for easier stable backports, this patch only adds a
vma_ops->closecheck to the buggy case 7 specifically. All other cases of vma removal are covered by thecan_vma_merge_before()check that includes the test forvma_ops->close.The reproducer code, adapted from Michal Hocko's code:
int
main(int argc, char *argv[]){
intsegment_id;
size_tsegment_size= 20 *PAGE_SIZE;
char *sh_mem;
structshmid_dsshmid_ds;key_t key = 0x1234; segment_id = shmget(key, segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); sh_mem = (char *)shmat(segment_id, NULL, 0); mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_NONE); mprotect(sh_mem + PAGE_SIZE, PAGE_SIZE, PROT_WRITE); mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_WRITE); shmdt(sh_mem); shmctl(segment_id, IPC_STAT, &shmid_ds); printf("nattch after shmdt(): %lu (expected: 0)\n", shmid_ds.shm_nattch);if (
shmctl(segment_id, IPC_RMID, 0))printf("IPCRM failed %d\n", errno);return (shmid_ds.shm_nattch) ? 1 : 0;
}Fixes 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in mergeability test")Signed-off-by Vlastimil Babka <[email protected]>Reported-by Michal Hocko <[email protected]>Cc Liam R. Howlett <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc <[email protected]>---
v2: deduplicate code, per Lorenzomm/mmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)mm/mmap.c +9
-2@@ -954,13 +954,21 @@ static struct vm_area_struct }else if (merge_prev) {/* case 2 */ if (curr) {vma_start_write (curr); - err =dup_anon_vma (prev, curr, &anon_dup);if (end == curr->vm_end) {/* case 7 */ +/* +* can_vma_merge_after() assumed we would not be +* removing prev vma, so it skipped the check +* for vm_ops->close, but we are removing curr +*/ +if (curr->vm_ops && curr->vm_ops->close) + err = -EINVAL ; remove = curr; }else {/* case 5 */ adjust = curr; adj_start = (end - curr->vm_start); } +if (!err) + err =dup_anon_vma (prev, curr, &anon_dup); } }else {/* merge_next */ vma_start_write (next); --
Quoted 2 lines
2.43.1
Looks good to me, feel free to add:
Quoted 58 lines · Vlastimil Babka
When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of
mprotect()operations can result in an elevatedshm_nattchand thus leak of the resource.The problem is caused by wrong assumptions in
vma_merge()commit714965ca8252("mm/mmap: start distinguishing if vma can be removed in mergeability test"). The shmem vmas have avma_ops->closecallback that decrementsshm_nattch, and we remove the vma without calling it.
vma_merge()has thus historically avoided merging vma's withvma_ops->closeand commit714965ca8252was supposed to keep it that way. It relaxed the checks forvma_ops->closeincan_vma_merge_after()assuming that it is never called on a vma that would be a candidate for removal. However, thevma_merge()code does also use the result of this check in the decision to remove a different vma in the merge case 7.A robust solution would be to refactor
vma_merge()code in a way that thevma_ops->closecheck is only done for vma's that are actually going to be removed, and not as part of the preliminary checks. That would both solve the existing bug, and also allow additional merges that the checks currently prevent unnecessarily in some cases.However to fix the existing bug first with a minimized risk, and for easier stable backports, this patch only adds a
vma_ops->closecheck to the buggy case 7 specifically. All other cases of vma removal are covered by thecan_vma_merge_before()check that includes the test forvma_ops->close.The reproducer code, adapted from Michal Hocko's code:
int
main(int argc, char *argv[]){
intsegment_id;
size_tsegment_size= 20 *PAGE_SIZE;
char *sh_mem;
structshmid_dsshmid_ds;key_t key = 0x1234; segment_id = shmget(key, segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); sh_mem = (char *)shmat(segment_id, NULL, 0); mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_NONE); mprotect(sh_mem + PAGE_SIZE, PAGE_SIZE, PROT_WRITE); mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_WRITE); shmdt(sh_mem); shmctl(segment_id, IPC_STAT, &shmid_ds); printf("nattch after shmdt(): %lu (expected: 0)\n", shmid_ds.shm_nattch);if (
shmctl(segment_id, IPC_RMID, 0))printf("IPCRM failed %d\n", errno);return (shmid_ds.shm_nattch) ? 1 : 0;
}
Quoted 6 lines
Fixes 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in mergeability test")Signed-off-by Vlastimil Babka <[email protected]>Reported-by Michal Hocko <[email protected]>Cc Liam R. Howlett <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc <[email protected]>
Quoted 35 lines
---
v2: deduplicate code, per Lorenzomm/mmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)mm/mmap.c +9
-1@@ -954,13 +954,21 @@ static struct vm_area_struct }else if (merge_prev) {/* case 2 */ if (curr) {vma_start_write (curr); - err =dup_anon_vma (prev, curr, &anon_dup);if (end == curr->vm_end) {/* case 7 */ +/* +* can_vma_merge_after() assumed we would not be +* removing prev vma, so it skipped the check +* for vm_ops->close, but we are removing curr +*/ +if (curr->vm_ops && curr->vm_ops->close) + err = -EINVAL ; remove = curr; }else {/* case 5 */ adjust = curr; adj_start = (end - curr->vm_start); } +if (!err) + err =dup_anon_vma (prev, curr, &anon_dup); } }else {/* merge_next */ vma_start_write (next);Signature · 1 line
2.43.1
PATCH v2 1/1 mm, mmap: fix vma_merge() case 7 with vma_ops->close
In reply to Vlastimil BabkaHi ltp,
I assume you'd like to turn the reproducer (from Michal Hocko) to a LTP test?
Cheers,
Vlastimil
-------- Forwarded Message --------
Subject: [PATCH v2] mm, mmap: fix vma_merge() case 7 with vma_ops->close
Date: Thu, 22 Feb 2024 22:59:31 +0100
From: Vlastimil Babka <[email protected]>
To: Andrew Morton <[email protected]>
CC: [email protected], [email protected], Vlastimil Babka <[email protected]>, Michal Hocko <[email protected]>, Liam R . Howlett <[email protected]>, Lorenzo Stoakes <[email protected]>, [email protected]
When debugging issues with a workload using SysV shmem, Michal Hocko has come up with a reproducer that shows how a series of mprotect() operations can result in an elevated shm_nattch and thus leak of the resource.
The problem is caused by wrong assumptions in vma_merge() commit 714965ca8252 ("mm/mmap: start distinguishing if vma can be removed in mergeability test"). The shmem vmas have a vma_ops->close callback that decrements shm_nattch, and we remove the vma without calling it.
vma_merge() has thus historically avoided merging vma's with vma_ops->close and commit 714965ca8252 was supposed to keep it that way. It relaxed the checks for vma_ops->close in can_vma_merge_after() assuming that it is never called on a vma that would be a candidate for removal. However, the vma_merge() code does also use the result of this check in the decision to remove a different vma in the merge case 7.
A robust solution would be to refactor vma_merge() code in a way that the vma_ops->close check is only done for vma's that are actually going to be removed, and not as part of the preliminary checks. That would both solve the existing bug, and also allow additional merges that the checks currently prevent unnecessarily in some cases.
However to fix the existing bug first with a minimized risk, and for easier stable backports, this patch only adds a vma_ops->close check to the buggy case 7 specifically. All other cases of vma removal are covered by the can_vma_merge_before() check that includes the test for vma_ops->close.
The reproducer code, adapted from Michal Hocko's code:
int main(int argc, char *argv[]) {
int segment_id;
size_t segment_size = 20 * PAGE_SIZE;
char * sh_mem;
struct shmid_ds shmid_ds;
key_t key = 0x1234;
segment_id = shmget(key, segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
sh_mem = (char *)shmat(segment_id, NULL, 0);
mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_NONE);
mprotect(sh_mem + PAGE_SIZE, PAGE_SIZE, PROT_WRITE);
mprotect(sh_mem + 2*PAGE_SIZE, PAGE_SIZE, PROT_WRITE);
shmdt(sh_mem);
shmctl(segment_id, IPC_STAT, &shmid_ds);
printf("nattch after shmdt(): %lu (expected: 0)\n", shmid_ds.shm_nattch); if (shmctl(segment_id, IPC_RMID, 0))
printf("IPCRM failed %d\n", errno); return (shmid_ds.shm_nattch) ? 1 : 0;
}
mergeability test")
---
v2: deduplicate code, per Lorenzo
mm/mmap.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
mm/mmap.c +9 -1
@@ -954,13 +954,21 @@ static struct vm_area_struct }else if (merge_prev) {/* case 2 */ if (curr) {vma_start_write (curr); - err =dup_anon_vma (prev, curr, &anon_dup);if (end == curr->vm_end) {/* case 7 */ +/* +* can_vma_merge_after() assumed we would not be +* removing prev vma, so it skipped the check +* for vm_ops->close, but we are removing curr +*/ +if (curr->vm_ops && curr->vm_ops->close) + err = -EINVAL ; remove = curr; }else {/* case 5 */ adjust = curr; adj_start = (end - curr->vm_start); } +if (!err) + err =dup_anon_vma (prev, curr, &anon_dup); } }else {/* merge_next */ vma_start_write (next);
Signature · 5 lines
2.43.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 1 line
I assume you'd like to turn the reproducer (from Michal Hocko) to a LTP test?
Already work-in-progress:
http://patchwork.ozlabs.org/project/ltp/patch/[email protected]/
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp