Skip to contentSkip to conversation

mm/madvise: remove CAP_SYS_ADMIN requirement for process_madvise(MADV_COLLAPSE)

PATCH In review Zach O'Keefe 3 participants 6 messages Aug 1, 2022 – Aug 4, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/madvise: remove CAP_SYS_ADMIN requirement for process_madvise(MADV_COLLAPSE)

process_madvise(MADV_COLLAPSE) currently requires CAP_SYS_ADMIN when not acting on the caller's own mm. This is maximally restrictive, and perpetuates existing issues with CAP_SYS_ADMIN. Remove this requirement.

When acting on an external process' memory, the biggest concerns for process_madvise(MADV_COLLAPSE) are (1) being able to influence process performance by moving memory, possibly between nodes, that is mapped into the address space of external process(es), (2) defeat of address-space-layout randomization, and (3), being able to increase process RSS and memcg usage, possibly causing memcg OOM.

process_madvise(2) already enforces CAP_SYS_NICE and PTRACE_MODE_READ (in PTRACE_MODE_FSCREDS mode). A process with these credentials can already accomplish (1) and (2) via move_pages(MPOL_MF_MOVE_ALL), and (3) via process_madvise(MADV_WILLNEED).

process_madvise(MADV_COLLAPSE) may also circumvent sysfs THP settings. When acting on one's own memory (which is equivalent to madvise(MADV_COLLAPSE)), this is deemed acceptable, since aside from the possibility of hoarding available hugepages (which is currently already possible) no harm to the system can be done. When acting on an external process' memory, circumventing sysfs THP settings should provide no additional threat compared to the ones listed. As such, imposing additional capabilities (such as CAP_SETUID, as a way to ensure the caller could have just altered the sysfs THP settings themselves) provides no extra protection.

Fixes 7ec952341312 ("mm/madvise: add MADV_COLLAPSE to process_madvise()")
---
 mm/madvise.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/madvise.c +3 -5
index f9e11b6c9916..af97100a0727 100644
@@ -1170,16 +1170,14 @@ madvise_behavior_valid(int behavior)
 	}
 }
 
-static bool
-process_madvise_behavior_valid(int behavior, struct task_struct *task)
+static bool process_madvise_behavior_valid(int behavior)
 {
 	switch (behavior) {
 	case MADV_COLD:
 	case MADV_PAGEOUT:
 	case MADV_WILLNEED:
-		return true;
 	case MADV_COLLAPSE:
-		return task == current || capable(CAP_SYS_ADMIN);
+		return true;
 	default:
 		return false;
 	}
@@ -1457,7 +1455,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
 		goto free_iov;
 	}
 
-	if (!process_madvise_behavior_valid(behavior, task)) {
+	if (!process_madvise_behavior_valid(behavior)) {
 		ret = -EINVAL;
 		goto release_task;
 	}
Signature · 1 line
2.37.1.455.g008518b4e5-goog
#1 lore raw

Hasn't this been discussed during the MADV_COLLAPSE submission? What has changed? Does this need more time to settle with the consensus?

Quoted 67 lines · Zach O'Keefe

process_madvise(MADV_COLLAPSE) currently requires CAP_SYS_ADMIN when not acting on the caller's own mm. This is maximally restrictive, and perpetuates existing issues with CAP_SYS_ADMIN. Remove this requirement.

When acting on an external process' memory, the biggest concerns for process_madvise(MADV_COLLAPSE) are (1) being able to influence process performance by moving memory, possibly between nodes, that is mapped into the address space of external process(es), (2) defeat of address-space-layout randomization, and (3), being able to increase process RSS and memcg usage, possibly causing memcg OOM.

process_madvise(2) already enforces CAP_SYS_NICE and PTRACE_MODE_READ (in PTRACE_MODE_FSCREDS mode). A process with these credentials can already accomplish (1) and (2) via move_pages(MPOL_MF_MOVE_ALL), and (3) via process_madvise(MADV_WILLNEED).

process_madvise(MADV_COLLAPSE) may also circumvent sysfs THP settings. When acting on one's own memory (which is equivalent to madvise(MADV_COLLAPSE)), this is deemed acceptable, since aside from the possibility of hoarding available hugepages (which is currently already possible) no harm to the system can be done. When acting on an external process' memory, circumventing sysfs THP settings should provide no additional threat compared to the ones listed. As such, imposing additional capabilities (such as CAP_SETUID, as a way to ensure the caller could have just altered the sysfs THP settings themselves) provides no extra protection.

Fixes 7ec952341312 ("mm/madvise: add MADV_COLLAPSE to process_madvise()")
---
 mm/madvise.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/madvise.c +3 -5
index f9e11b6c9916..af97100a0727 100644
@@ -1170,16 +1170,14 @@ madvise_behavior_valid(int behavior)
 	}
 }
 
-static bool
-process_madvise_behavior_valid(int behavior, struct task_struct *task)
+static bool process_madvise_behavior_valid(int behavior)
 {
 	switch (behavior) {
 	case MADV_COLD:
 	case MADV_PAGEOUT:
 	case MADV_WILLNEED:
-		return true;
 	case MADV_COLLAPSE:
-		return task == current || capable(CAP_SYS_ADMIN);
+		return true;
 	default:
 		return false;
 	}
@@ -1457,7 +1455,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
 		goto free_iov;
 	}
 
-	if (!process_madvise_behavior_valid(behavior, task)) {
+	if (!process_madvise_behavior_valid(behavior)) {
 		ret = -EINVAL;
 		goto release_task;
 	}
Signature · 1 line
2.37.1.455.g008518b4e5-goog
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 65 lines · Michal Hocko

Hasn't this been discussed during the MADV_COLLAPSE submission? What has changed? Does this need more time to settle with the consensus?

Quoted 60 lines · Zach O'Keefe

process_madvise(MADV_COLLAPSE) currently requires CAP_SYS_ADMIN when not acting on the caller's own mm. This is maximally restrictive, and perpetuates existing issues with CAP_SYS_ADMIN. Remove this requirement.

When acting on an external process' memory, the biggest concerns for process_madvise(MADV_COLLAPSE) are (1) being able to influence process performance by moving memory, possibly between nodes, that is mapped into the address space of external process(es), (2) defeat of address-space-layout randomization, and (3), being able to increase process RSS and memcg usage, possibly causing memcg OOM.

process_madvise(2) already enforces CAP_SYS_NICE and PTRACE_MODE_READ (in PTRACE_MODE_FSCREDS mode). A process with these credentials can already accomplish (1) and (2) via move_pages(MPOL_MF_MOVE_ALL), and (3) via process_madvise(MADV_WILLNEED).

process_madvise(MADV_COLLAPSE) may also circumvent sysfs THP settings. When acting on one's own memory (which is equivalent to madvise(MADV_COLLAPSE)), this is deemed acceptable, since aside from the possibility of hoarding available hugepages (which is currently already possible) no harm to the system can be done. When acting on an external process' memory, circumventing sysfs THP settings should provide no additional threat compared to the ones listed. As such, imposing additional capabilities (such as CAP_SETUID, as a way to ensure the caller could have just altered the sysfs THP settings themselves) provides no extra protection.

Fixes 7ec952341312 ("mm/madvise: add MADV_COLLAPSE to process_madvise()")
---
 mm/madvise.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/madvise.c +2 -4
index f9e11b6c9916..af97100a0727 100644
@@ -1170,16 +1170,14 @@ madvise_behavior_valid(int behavior)
      }
 }
 
-static bool
-process_madvise_behavior_valid(int behavior, struct task_struct *task)
+static bool process_madvise_behavior_valid(int behavior)
 {
      switch (behavior) {
      case MADV_COLD:
      case MADV_PAGEOUT:
      case MADV_WILLNEED:
-             return true;
      case MADV_COLLAPSE:
-             return task == current || capable(CAP_SYS_ADMIN);
+             return true;
      default:
              return false;
      }
@@ -1457,7 +1455,7 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __user *, vec,
              goto free_iov;
      }
 
Quoted 11 lines
Quoted 7 lines
mm/madvise.c +1 -2
-     if (!process_madvise_behavior_valid(behavior, task)) {
+     if (!process_madvise_behavior_valid(behavior)) {
              ret = -EINVAL;
              goto release_task;
      }
--

2.37.1.455.g008518b4e5-goog

--
Michal Hocko
SUSE Labs

Hey Michal,

Thanks for taking the time to take a look at this.

"mm/madvise: add MADV_COLLAPSE to process_madvise()" in the v7 series ended with me mentioning a couple options, but ultimately I didn't present a solution, and no consensus was reached[1]. After taking a closer look, this is my proposal for what I believe to be the best path forward. It should be squashed into the original patch. What do you think?

Thanks again,
Zach

[1] https://lore.kernel.org/linux-mm/[email protected]/

#3 lore raw

On Tue 02-08-22 02:48:33, Zach O'Keefe wrote:
[...]

Quoted 5 lines

"mm/madvise: add MADV_COLLAPSE to process_madvise()" in the v7 series ended with me mentioning a couple options, but ultimately I didn't present a solution, and no consensus was reached[1]. After taking a closer look, this is my proposal for what I believe to be the best path forward. It should be squashed into the original patch. What do you think?

If it is agreed that the CAP_SYS_ADMIN is too strict of a requirement then yes, this should be squashed into the original patch. There is no real reason to create a potential bisection headache by changing the permission model in a later patch.

From my POV, I would agree that CAP_SYS_ADMIN is just too strict of a requirement.

I didn't really have time to follow recent discussions but I would argue that the operation is not really destructive or seriously harmful. All applications can already have their memory (almost) equally THP collapsed by khupaged with the proposed process_madvise semantic.

NOHUGEMEM and prctl opt out from THP are both honored AFAIU and the only difference is the global THP killswitch behavior which I do not think warrants the strongest CAP_SYS_ADMIN capability (especially because it doesn't really control all kinds of THPs).

If there is a userspace agent collapsing memory and causing problems then it can be easily fixed in the userspace. And I find that easier to do than putting the bar so high that userspace agents would be unfeasible because of CAP_SYS_ADMIN (which is nono in many cases as it would allow essentially full control of other stuff). So from practical POV, risking an extended RSS is really a negligible risk to lose a potentially useful feature for all others.

Just my 2c

Quoted 4 lines

Thanks again,
Zach

[1] https://lore.kernel.org/linux-mm/[email protected]/

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 9 lines · Michal Hocko

On Tue 02-08-22 02:48:33, Zach O'Keefe wrote:
[...]

Quoted 5 lines

"mm/madvise: add MADV_COLLAPSE to process_madvise()" in the v7 series ended with me mentioning a couple options, but ultimately I didn't present a solution, and no consensus was reached[1]. After taking a closer look, this is my proposal for what I believe to be the best path forward. It should be squashed into the original patch. What do you think?

Quoted 4 lines

If it is agreed that the CAP_SYS_ADMIN is too strict of a requirement then yes, this should be squashed into the original patch. There is no real reason to create a potential bisection headache by changing the permission model in a later patch.

Sorry about the confusion here. Assumed (incorrectly) that Andrew would kindly squash this in mm-unstable since I added the Fixes: tag. Next time I'll add some explicit verbiage saying it should be squashed.

Quoted 8 lines

From my POV, I would agree that CAP_SYS_ADMIN is just too strict of a requirement.

I didn't really have time to follow recent discussions but I would argue that the operation is not really destructive or seriously harmful. All applications can already have their memory (almost) equally THP collapsed by khupaged with the proposed process_madvise semantic.

Quoted 4 lines

NOHUGEMEM and prctl opt out from THP are both honored AFAIU and the only difference is the global THP killswitch behavior which I do not think warrants the strongest CAP_SYS_ADMIN capability (especially because it doesn't really control all kinds of THPs).

Ya. In fact, I don't think the ignoring the THP sysfs controls warrants any additional capability (set alone CAPS_SYS_ADMIN), since a malicious program can't really inflict any more damage than they would with CAP_SYS_NICE and PTRACE_MODE_READ.

Quoted 8 lines

If there is a userspace agent collapsing memory and causing problems then it can be easily fixed in the userspace. And I find that easier to do than putting the bar so high that userspace agents would be unfeasible because of CAP_SYS_ADMIN (which is nono in many cases as it would allow essentially full control of other stuff). So from practical POV, risking an extended RSS is really a negligible risk to lose a potentially useful feature for all others.

Agreed.

Thanks for taking the time, Michal!
Zach

Quoted 10 lines

Just my 2c

Quoted 4 lines

Thanks again,
Zach

[1] https://lore.kernel.org/linux-mm/[email protected]/

--
Michal Hocko
SUSE Labs

#5 lore raw
Quoted 48 lines · Zach O'Keefe
Quoted 9 lines · Michal Hocko

On Tue 02-08-22 02:48:33, Zach O'Keefe wrote:
[...]

Quoted 5 lines

"mm/madvise: add MADV_COLLAPSE to process_madvise()" in the v7 series ended with me mentioning a couple options, but ultimately I didn't present a solution, and no consensus was reached[1]. After taking a closer look, this is my proposal for what I believe to be the best path forward. It should be squashed into the original patch. What do you think?

Quoted 4 lines

If it is agreed that the CAP_SYS_ADMIN is too strict of a requirement then yes, this should be squashed into the original patch. There is no real reason to create a potential bisection headache by changing the permission model in a later patch.

Sorry about the confusion here. Assumed (incorrectly) that Andrew would kindly squash this in mm-unstable since I added the Fixes: tag. Next time I'll add some explicit verbiage saying it should be squashed.

Quoted 8 lines

From my POV, I would agree that CAP_SYS_ADMIN is just too strict of a requirement.

I didn't really have time to follow recent discussions but I would argue that the operation is not really destructive or seriously harmful. All applications can already have their memory (almost) equally THP collapsed by khupaged with the proposed process_madvise semantic.

Quoted 4 lines

NOHUGEMEM and prctl opt out from THP are both honored AFAIU and the only difference is the global THP killswitch behavior which I do not think warrants the strongest CAP_SYS_ADMIN capability (especially because it doesn't really control all kinds of THPs).

Ya. In fact, I don't think the ignoring the THP sysfs controls warrants any additional capability (set alone CAPS_SYS_ADMIN), since a malicious program can't really inflict any more damage than they would with CAP_SYS_NICE and PTRACE_MODE_READ.

Quoted 8 lines

If there is a userspace agent collapsing memory and causing problems then it can be easily fixed in the userspace. And I find that easier to do than putting the bar so high that userspace agents would be unfeasible because of CAP_SYS_ADMIN (which is nono in many cases as it would allow essentially full control of other stuff). So from practical POV, risking an extended RSS is really a negligible risk to lose a potentially useful feature for all others.

Quoted 1 line

Agreed.

+1

Quoted 15 lines

Thanks for taking the time, Michal!
Zach

Quoted 10 lines

Just my 2c

Quoted 4 lines

Thanks again,
Zach

[1] https://lore.kernel.org/linux-mm/[email protected]/

--
Michal Hocko
SUSE Labs