diff mbox series

[v4,1/2] proc: pass file instead of inode to proc_mem_open

Message ID 20240524192858.3206-1-adrian.ratiu@collabora.com (mailing list archive)
State Superseded
Headers show
Series [v4,1/2] proc: pass file instead of inode to proc_mem_open | expand

Commit Message

Adrian Ratiu May 24, 2024, 7:28 p.m. UTC
The file struct is required in proc_mem_open() so its
f_mode can be checked when deciding whether to allow or
deny /proc/*/mem open requests via the new read/write
and foll_force restriction mechanism.

Thus instead of directly passing the inode to the fun,
we pass the file and get the inode inside it.

Cc: Jann Horn <jannh@google.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Christian Brauner <brauner@kernel.org>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
* New in v4
---
 fs/proc/base.c     | 6 +++---
 fs/proc/internal.h | 2 +-
 fs/proc/task_mmu.c | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

Comments

kernel test robot May 24, 2024, 9:05 p.m. UTC | #1
Hi Adrian,

kernel test robot noticed the following build errors:

[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on kees/for-next/kspp linus/master v6.9 next-20240523]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Adrian-Ratiu/proc-restrict-proc-pid-mem/20240525-033201
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore
patch link:    https://lore.kernel.org/r/20240524192858.3206-1-adrian.ratiu%40collabora.com
patch subject: [PATCH v4 1/2] proc: pass file instead of inode to proc_mem_open
config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20240525/202405250413.EENbErWw-lkp@intel.com/config)
compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project 7aa382fd7257d9bd4f7fc50bb7078a3c26a1628c)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240525/202405250413.EENbErWw-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202405250413.EENbErWw-lkp@intel.com/

All errors (new ones prefixed by >>):

   In file included from fs/proc/task_nommu.c:3:
   In file included from include/linux/mm.h:2208:
   include/linux/vmstat.h:522:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     522 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
>> fs/proc/task_nommu.c:262:27: error: incompatible pointer types passing 'struct inode *' to parameter of type 'struct file *' [-Werror,-Wincompatible-pointer-types]
     262 |         priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
         |                                  ^~~~~
   fs/proc/internal.h:298:46: note: passing argument to parameter 'file' here
     298 | struct mm_struct *proc_mem_open(struct file *file, unsigned int mode);
         |                                              ^
   1 warning and 1 error generated.


vim +262 fs/proc/task_nommu.c

b76437579d1344 Siddhesh Poyarekar 2012-03-21  251  
b76437579d1344 Siddhesh Poyarekar 2012-03-21  252  static int maps_open(struct inode *inode, struct file *file,
b76437579d1344 Siddhesh Poyarekar 2012-03-21  253  		     const struct seq_operations *ops)
662795deb854b3 Eric W. Biederman  2006-06-26  254  {
dbf8685c8e2140 David Howells      2006-09-27  255  	struct proc_maps_private *priv;
dbf8685c8e2140 David Howells      2006-09-27  256  
27692cd56e2aa6 Oleg Nesterov      2014-10-09  257  	priv = __seq_open_private(file, ops, sizeof(*priv));
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  258  	if (!priv)
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  259  		return -ENOMEM;
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  260  
2c03376d2db005 Oleg Nesterov      2014-10-09  261  	priv->inode = inode;
27692cd56e2aa6 Oleg Nesterov      2014-10-09 @262  	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  263  	if (IS_ERR(priv->mm)) {
27692cd56e2aa6 Oleg Nesterov      2014-10-09  264  		int err = PTR_ERR(priv->mm);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  265  
27692cd56e2aa6 Oleg Nesterov      2014-10-09  266  		seq_release_private(inode, file);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  267  		return err;
27692cd56e2aa6 Oleg Nesterov      2014-10-09  268  	}
27692cd56e2aa6 Oleg Nesterov      2014-10-09  269  
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  270  	return 0;
662795deb854b3 Eric W. Biederman  2006-06-26  271  }
662795deb854b3 Eric W. Biederman  2006-06-26  272
kernel test robot May 24, 2024, 9:56 p.m. UTC | #2
Hi Adrian,

kernel test robot noticed the following build errors:

[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on kees/for-next/kspp linus/master v6.9 next-20240523]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Adrian-Ratiu/proc-restrict-proc-pid-mem/20240525-033201
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore
patch link:    https://lore.kernel.org/r/20240524192858.3206-1-adrian.ratiu%40collabora.com
patch subject: [PATCH v4 1/2] proc: pass file instead of inode to proc_mem_open
config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20240525/202405250507.0z0WHg9L-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240525/202405250507.0z0WHg9L-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202405250507.0z0WHg9L-lkp@intel.com/

All errors (new ones prefixed by >>):

   fs/proc/task_nommu.c: In function 'maps_open':
>> fs/proc/task_nommu.c:262:34: error: passing argument 1 of 'proc_mem_open' from incompatible pointer type [-Werror=incompatible-pointer-types]
     262 |         priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
         |                                  ^~~~~
         |                                  |
         |                                  struct inode *
   In file included from fs/proc/task_nommu.c:13:
   fs/proc/internal.h:298:46: note: expected 'struct file *' but argument is of type 'struct inode *'
     298 | struct mm_struct *proc_mem_open(struct file *file, unsigned int mode);
         |                                 ~~~~~~~~~~~~~^~~~
   cc1: some warnings being treated as errors


vim +/proc_mem_open +262 fs/proc/task_nommu.c

b76437579d1344 Siddhesh Poyarekar 2012-03-21  251  
b76437579d1344 Siddhesh Poyarekar 2012-03-21  252  static int maps_open(struct inode *inode, struct file *file,
b76437579d1344 Siddhesh Poyarekar 2012-03-21  253  		     const struct seq_operations *ops)
662795deb854b3 Eric W. Biederman  2006-06-26  254  {
dbf8685c8e2140 David Howells      2006-09-27  255  	struct proc_maps_private *priv;
dbf8685c8e2140 David Howells      2006-09-27  256  
27692cd56e2aa6 Oleg Nesterov      2014-10-09  257  	priv = __seq_open_private(file, ops, sizeof(*priv));
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  258  	if (!priv)
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  259  		return -ENOMEM;
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  260  
2c03376d2db005 Oleg Nesterov      2014-10-09  261  	priv->inode = inode;
27692cd56e2aa6 Oleg Nesterov      2014-10-09 @262  	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  263  	if (IS_ERR(priv->mm)) {
27692cd56e2aa6 Oleg Nesterov      2014-10-09  264  		int err = PTR_ERR(priv->mm);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  265  
27692cd56e2aa6 Oleg Nesterov      2014-10-09  266  		seq_release_private(inode, file);
27692cd56e2aa6 Oleg Nesterov      2014-10-09  267  		return err;
27692cd56e2aa6 Oleg Nesterov      2014-10-09  268  	}
27692cd56e2aa6 Oleg Nesterov      2014-10-09  269  
ce34fddb5bafb4 Oleg Nesterov      2014-10-09  270  	return 0;
662795deb854b3 Eric W. Biederman  2006-06-26  271  }
662795deb854b3 Eric W. Biederman  2006-06-26  272
Kees Cook May 31, 2024, 9:14 p.m. UTC | #3
On Fri, May 24, 2024 at 10:28:57PM +0300, Adrian Ratiu wrote:
> The file struct is required in proc_mem_open() so its
> f_mode can be checked when deciding whether to allow or
> deny /proc/*/mem open requests via the new read/write
> and foll_force restriction mechanism.
> 
> Thus instead of directly passing the inode to the fun,
> we pass the file and get the inode inside it.
> 
> Cc: Jann Horn <jannh@google.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Christian Brauner <brauner@kernel.org>
> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>

With the nommu errors pointed out by 0day fixed:

Reviewed-by: Kees Cook <kees@kernel.org>
diff mbox series

Patch

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 18550c071d71..6faf1b3a4117 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -794,9 +794,9 @@  static const struct file_operations proc_single_file_operations = {
 };
 
 
-struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
+struct mm_struct *proc_mem_open(struct file  *file, unsigned int mode)
 {
-	struct task_struct *task = get_proc_task(inode);
+	struct task_struct *task = get_proc_task(file->f_inode);
 	struct mm_struct *mm = ERR_PTR(-ESRCH);
 
 	if (task) {
@@ -816,7 +816,7 @@  struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
 
 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
 {
-	struct mm_struct *mm = proc_mem_open(inode, mode);
+	struct mm_struct *mm = proc_mem_open(file, mode);
 
 	if (IS_ERR(mm))
 		return PTR_ERR(mm);
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index a71ac5379584..d38b2eea40d1 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -295,7 +295,7 @@  struct proc_maps_private {
 #endif
 } __randomize_layout;
 
-struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
+struct mm_struct *proc_mem_open(struct file *file, unsigned int mode);
 
 extern const struct file_operations proc_pid_maps_operations;
 extern const struct file_operations proc_pid_numa_maps_operations;
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index e5a5f015ff03..dc9abbf662be 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -210,7 +210,7 @@  static int proc_maps_open(struct inode *inode, struct file *file,
 		return -ENOMEM;
 
 	priv->inode = inode;
-	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
+	priv->mm = proc_mem_open(file, PTRACE_MODE_READ);
 	if (IS_ERR(priv->mm)) {
 		int err = PTR_ERR(priv->mm);
 
@@ -1025,7 +1025,7 @@  static int smaps_rollup_open(struct inode *inode, struct file *file)
 		goto out_free;
 
 	priv->inode = inode;
-	priv->mm = proc_mem_open(inode, PTRACE_MODE_READ);
+	priv->mm = proc_mem_open(file, PTRACE_MODE_READ);
 	if (IS_ERR(priv->mm)) {
 		ret = PTR_ERR(priv->mm);
 
@@ -1749,7 +1749,7 @@  static int pagemap_open(struct inode *inode, struct file *file)
 {
 	struct mm_struct *mm;
 
-	mm = proc_mem_open(inode, PTRACE_MODE_READ);
+	mm = proc_mem_open(file, PTRACE_MODE_READ);
 	if (IS_ERR(mm))
 		return PTR_ERR(mm);
 	file->private_data = mm;