Re: [PATCH RFC 4/5] mm/ksm, proc: introduce remote merge

From: Jann Horn
Date: Thu May 16 2019 - 06:02:48 EST


On Thu, May 16, 2019 at 11:43 AM Oleksandr Natalenko
<oleksandr@xxxxxxxxxx> wrote:
> Use previously introduced remote madvise knob to mark task's
> anonymous memory as mergeable.
>
> To force merging task's VMAs, "merge" hint is used:
>
> # echo merge > /proc/<pid>/madvise
>
> Force unmerging is done similarly:
>
> # echo unmerge > /proc/<pid>/madvise
>
> To achieve this, previously introduced ksm_madvise_*() helpers
> are used.

Why does this not require PTRACE_MODE_ATTACH_FSCREDS to the target
process? Enabling KSM on another process is hazardous because it
significantly increases the attack surface for side channels.

(Note that if you change this to require PTRACE_MODE_ATTACH_FSCREDS,
you'll want to use mm_access() in the ->open handler and drop the mm
in ->release. mm_access() from a ->write handler is not permitted.)

[...]
> @@ -2960,15 +2962,63 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
> static ssize_t madvise_write(struct file *file, const char __user *buf,
> size_t count, loff_t *ppos)
> {
> + /* For now, only KSM hints are implemented */
> +#ifdef CONFIG_KSM
> + char buffer[PROC_NUMBUF];
> + int behaviour;
> struct task_struct *task;
> + struct mm_struct *mm;
> + int err = 0;
> + struct vm_area_struct *vma;
> +
> + memset(buffer, 0, sizeof(buffer));
> + if (count > sizeof(buffer) - 1)
> + count = sizeof(buffer) - 1;
> + if (copy_from_user(buffer, buf, count))
> + return -EFAULT;
> +
> + if (!memcmp("merge", buffer, min(sizeof("merge")-1, count)))

This means that you also match on something like "mergeblah". Just use strcmp().

> + behaviour = MADV_MERGEABLE;
> + else if (!memcmp("unmerge", buffer, min(sizeof("unmerge")-1, count)))
> + behaviour = MADV_UNMERGEABLE;
> + else
> + return -EINVAL;
>
> task = get_proc_task(file_inode(file));
> if (!task)
> return -ESRCH;
>
> + mm = get_task_mm(task);
> + if (!mm) {
> + err = -EINVAL;
> + goto out_put_task_struct;
> + }
> +
> + down_write(&mm->mmap_sem);

Should a check for mmget_still_valid(mm) be inserted here? See commit
04f5866e41fb70690e28397487d8bd8eea7d712a.

> + switch (behaviour) {
> + case MADV_MERGEABLE:
> + case MADV_UNMERGEABLE:

This switch isn't actually necessary at this point, right?

> + vma = mm->mmap;
> + while (vma) {
> + if (behaviour == MADV_MERGEABLE)
> + ksm_madvise_merge(vma->vm_mm, vma, &vma->vm_flags);
> + else
> + ksm_madvise_unmerge(vma, vma->vm_start, vma->vm_end, &vma->vm_flags);
> + vma = vma->vm_next;
> + }
> + break;
> + }
> + up_write(&mm->mmap_sem);
> +
> + mmput(mm);
> +
> +out_put_task_struct:
> put_task_struct(task);
>
> - return count;
> + return err ? err : count;
> +#else
> + return -EINVAL;
> +#endif /* CONFIG_KSM */
> }