linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
@ 2020-08-20  2:18 linmiaohe
  2020-08-20 21:21 ` Kees Cook
  0 siblings, 1 reply; 6+ messages in thread
From: linmiaohe @ 2020-08-20  2:18 UTC (permalink / raw)
  To: Kees Cook; +Cc: akpm, linux-mm, linux-kernel, linux-security-module

Kees Cook <keescook@chromium.org> wrote:
>On Mon, Aug 17, 2020 at 07:59:33AM -0400, Miaohe Lin wrote:
>> There is no need to check if this process has the right to modify the 
>> specified process when they are same.
>> 
>> Signed-off-by: Hongxiang Lou <louhongxiang@huawei.com>
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>
>NAK, please don't do this -- the ptrace and security hooks already do these kinds of self-introspection checks, and I'd like to keep a central place to perform these kinds of checks.
>

Many thanks for your reply.
We also avoid get_task_struct/ put_task_struct pair of atomic ops, rcu_lock, task_lock and so on this way.

>Is there a specific problem you've encountered that this fixes?
>

I'am sorry but there's no specific problem. I do this mainly to skip the unnecessary ptrace and security hooks.

>--
>Kees Cook

Thanks again.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
  2020-08-20  2:18 [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages() linmiaohe
@ 2020-08-20 21:21 ` Kees Cook
  0 siblings, 0 replies; 6+ messages in thread
From: Kees Cook @ 2020-08-20 21:21 UTC (permalink / raw)
  To: linmiaohe; +Cc: akpm, linux-mm, linux-kernel, linux-security-module

On Thu, Aug 20, 2020 at 02:18:21AM +0000, linmiaohe wrote:
> Kees Cook <keescook@chromium.org> wrote:
> >On Mon, Aug 17, 2020 at 07:59:33AM -0400, Miaohe Lin wrote:
> >> There is no need to check if this process has the right to modify the 
> >> specified process when they are same.
> >> 
> >> Signed-off-by: Hongxiang Lou <louhongxiang@huawei.com>
> >> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> >
> >NAK, please don't do this -- the ptrace and security hooks already do these kinds of self-introspection checks, and I'd like to keep a central place to perform these kinds of checks.
> >
> 
> Many thanks for your reply.
> We also avoid get_task_struct/ put_task_struct pair of atomic ops, rcu_lock, task_lock and so on this way.
> 
> >Is there a specific problem you've encountered that this fixes?
> >
> 
> I'am sorry but there's no specific problem. I do this mainly to skip the unnecessary ptrace and security hooks.

Cool. Let's keep this as-is so we continue to have centralized
instrumentation of these things in the LSM. :)

Thanks for your attention to performance!

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
  2020-08-17 11:59 Miaohe Lin
  2020-08-17 12:32 ` Matthew Wilcox
@ 2020-08-19 18:04 ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2020-08-19 18:04 UTC (permalink / raw)
  To: Miaohe Lin; +Cc: akpm, linux-mm, linux-kernel, linux-security-module

On Mon, Aug 17, 2020 at 07:59:33AM -0400, Miaohe Lin wrote:
> There is no need to check if this process has the right to modify the
> specified process when they are same.
> 
> Signed-off-by: Hongxiang Lou <louhongxiang@huawei.com>
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
>  mm/migrate.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 34a842a8eb6a..342c1ce0b433 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -1903,7 +1903,7 @@ static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
>  	 * Check if this process has the right to modify the specified
>  	 * process. Use the regular "ptrace_may_access()" checks.
>  	 */
> -	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
> +	if (pid && !ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
>  		rcu_read_unlock();
>  		err = -EPERM;
>  		goto out;

NAK, please don't do this -- the ptrace and security hooks already do
these kinds of self-introspection checks, and I'd like to keep a central
place to perform these kinds of checks.

Is there a specific problem you've encountered that this fixes?

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
@ 2020-08-18  2:25 linmiaohe
  0 siblings, 0 replies; 6+ messages in thread
From: linmiaohe @ 2020-08-18  2:25 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: akpm, linux-mm, linux-kernel

Matthew Wilcox <willy@infradead.org> wrote:
>On Mon, Aug 17, 2020 at 07:59:33AM -0400, Miaohe Lin wrote:
>> There is no need to check if this process has the right to modify the 
>> specified process when they are same.
>
>We should probably also skip the security hook call if a process is modifying its own pages.
>
>How about this instead?
>

It sounds good, thanks for your good advice. Would you like a tag like suggest-by?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
  2020-08-17 11:59 Miaohe Lin
@ 2020-08-17 12:32 ` Matthew Wilcox
  2020-08-19 18:04 ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2020-08-17 12:32 UTC (permalink / raw)
  To: Miaohe Lin; +Cc: akpm, linux-mm, linux-kernel

On Mon, Aug 17, 2020 at 07:59:33AM -0400, Miaohe Lin wrote:
> There is no need to check if this process has the right to modify the
> specified process when they are same.

We should probably also skip the security hook call if a process is
modifying its own pages.

How about this instead?

diff --git a/mm/migrate.c b/mm/migrate.c
index f37729673558..84dc1f42161d 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1797,33 +1797,22 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
 	return nr_pages ? -EFAULT : 0;
 }
 
-/*
- * Move a list of pages in the address space of the currently executing
- * process.
- */
-static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
-			     const void __user * __user *pages,
-			     const int __user *nodes,
-			     int __user *status, int flags)
+static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 {
 	struct task_struct *task;
 	struct mm_struct *mm;
-	int err;
-	nodemask_t task_nodes;
-
-	/* Check flags */
-	if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
-		return -EINVAL;
 
-	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
-		return -EPERM;
+	if (!pid) {
+		mmget(current->mm);
+		*mem_nodes = cpuset_mems_allowed(current);
+		return current->mm;
+	}
 
-	/* Find the mm_struct */
 	rcu_read_lock();
-	task = pid ? find_task_by_vpid(pid) : current;
+	task = find_task_by_vpid(pid);
 	if (!task) {
 		rcu_read_unlock();
-		return -ESRCH;
+		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
 
@@ -1833,22 +1822,49 @@ static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
 		rcu_read_unlock();
-		err = -EPERM;
+		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
 	rcu_read_unlock();
 
- 	err = security_task_movememory(task);
- 	if (err)
+	mm = ERR_PTR(security_task_movememory(task));
+	if (IS_ERR(mm))
 		goto out;
 
-	task_nodes = cpuset_mems_allowed(task);
+	*mem_nodes = cpuset_mems_allowed(task);
 	mm = get_task_mm(task);
+out:
 	put_task_struct(task);
 
 	if (!mm)
+		mm = ERR_PTR(-EINVAL);
+	return mm;
+}
+
+/*
+ * Move a list of pages in the address space of the specified
+ * process (pid of 0 means current).
+ */
+static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
+			     const void __user * __user *pages,
+			     const int __user *nodes,
+			     int __user *status, int flags)
+{
+	struct mm_struct *mm;
+	int err;
+	nodemask_t task_nodes;
+
+	/* Check flags */
+	if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
 		return -EINVAL;
 
+	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
+		return -EPERM;
+
+	mm = find_mm_struct(pid, &task_nodes);
+	if (IS_ERR(mm))
+		return PTR_ERR(mm);
+
 	if (nodes)
 		err = do_pages_move(mm, task_nodes, nr_pages, pages,
 				    nodes, status, flags);
@@ -1857,10 +1873,6 @@ static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
 
 	mmput(mm);
 	return err;
-
-out:
-	put_task_struct(task);
-	return err;
 }
 
 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages()
@ 2020-08-17 11:59 Miaohe Lin
  2020-08-17 12:32 ` Matthew Wilcox
  2020-08-19 18:04 ` Kees Cook
  0 siblings, 2 replies; 6+ messages in thread
From: Miaohe Lin @ 2020-08-17 11:59 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, linmiaohe

There is no need to check if this process has the right to modify the
specified process when they are same.

Signed-off-by: Hongxiang Lou <louhongxiang@huawei.com>
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 mm/migrate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/migrate.c b/mm/migrate.c
index 34a842a8eb6a..342c1ce0b433 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1903,7 +1903,7 @@ static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.
 	 */
-	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
+	if (pid && !ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
 		rcu_read_unlock();
 		err = -EPERM;
 		goto out;
-- 
2.19.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-08-20 21:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-20  2:18 [PATCH] mm/migrate: Avoid possible unnecessary ptrace_may_access() call in kernel_move_pages() linmiaohe
2020-08-20 21:21 ` Kees Cook
  -- strict thread matches above, loose matches on Subject: below --
2020-08-18  2:25 linmiaohe
2020-08-17 11:59 Miaohe Lin
2020-08-17 12:32 ` Matthew Wilcox
2020-08-19 18:04 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).