/proc//ksm_merging_pages and /proc//ksm_stat can only be opened by the owner of the task but they show the mm the task has at the time of the read. So a file that was opened while the task ran as its opener keeps working after it executed a set-user-ID program. PR_SET_MEMORY_MERGE survives execve(). So the opener can have the memory of the privileged program merged and find out how many of its pages were merged with pages of known content, which is what the mode of these files is supposed to prevent. Use the exec id helpers and hold exec_update_lock until the mm has been looked up. The files need their own file operations for that so the exec id can live in ->private. Reading a file that was opened before an execve() now fails with -EACCES if its opener has no access to the new program. Reported-by: Josef Bacik Fixes: 7609385337a4 ("ksm: count ksm merging pages for each process") Fixes: cb4df4cae4f2 ("ksm: count allocated ksm rmap_items for each process") Signed-off-by: Christian Brauner (Amutable) --- fs/proc/base.c | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 600cdbfdefac..57748b4ae806 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -182,6 +182,10 @@ struct pid_entry { NOD(NAME, (S_IFREG|(MODE)), \ NULL, &proc_single_file_operations, \ { .proc_show = show } ) +#define ONE_EXEC(NAME, MODE, show) \ + NOD(NAME, (S_IFREG|(MODE)), \ + NULL, &proc_single_exec_file_operations, \ + { .proc_show = show } ) #define ATTR(LSMID, NAME, MODE) \ NOD(NAME, (S_IFREG|(MODE)), \ NULL, &proc_pid_attr_operations, \ @@ -3360,12 +3364,42 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, #endif /* CONFIG_LIVEPATCH */ #ifdef CONFIG_KSM +/* Like proc_single_file_operations, but the file keeps the exec id. */ +static int proc_single_exec_open(struct inode *inode, struct file *filp) +{ + return proc_exec_id_single_open(inode, filp, proc_single_show); +} + +static const struct file_operations proc_single_exec_file_operations = { + .open = proc_single_exec_open, + .read = seq_read, + .llseek = seq_lseek, + .release = proc_exec_id_single_release, +}; + +/* get_task_mm(), unless the task now runs a program the opener can't see */ +static struct mm_struct *proc_single_exec_mm(struct seq_file *m, + struct task_struct *task) +{ + struct mm_struct *mm; + int err; + + err = proc_exec_id_lock(m->file, task, m->private); + if (err) + return ERR_PTR(err); + mm = get_task_mm(task); + proc_exec_id_unlock(task); + return mm; +} + static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { struct mm_struct *mm; - mm = get_task_mm(task); + mm = proc_single_exec_mm(m, task); + if (IS_ERR(mm)) + return PTR_ERR(mm); if (mm) { seq_printf(m, "%lu\n", mm->ksm_merging_pages); mmput(mm); @@ -3379,7 +3413,9 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, struct mm_struct *mm; int ret = 0; - mm = get_task_mm(task); + mm = proc_single_exec_mm(m, task); + if (IS_ERR(mm)) + return PTR_ERR(mm); if (mm) { seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm)); @@ -3532,8 +3568,8 @@ static const struct pid_entry tgid_base_stuff[] = { ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif #ifdef CONFIG_KSM - ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), - ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat), + ONE_EXEC("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), + ONE_EXEC("ksm_stat", S_IRUSR, proc_pid_ksm_stat), #endif }; @@ -3877,8 +3913,8 @@ static const struct pid_entry tid_base_stuff[] = { ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache), #endif #ifdef CONFIG_KSM - ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), - ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat), + ONE_EXEC("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), + ONE_EXEC("ksm_stat", S_IRUSR, proc_pid_ksm_stat), #endif }; -- 2.53.0