Skip to contentSkip to conversation

genirq: procfs: Make smp_affinity read-only for interrupts that userspace can't set

PATCH Applied Jeff Xie 4 participants 8 messages Aug 25, 2024 – Aug 29, 2024 IRQ

lore
Share & tools
lore

Jeff Xie

author
#0 lore raw

PATCH v2 1/1 genirq: procfs: Make smp_affinity read-only for interrupts that userspace can't set

The kernel already knows at the time of interrupt allocation that the affinity cannot be controlled by userspace and therefore creating the file with write permissions is wrong.

Therefore set the file permissions to read-only for such interrupts.

---
v2:
- Updated the description suggested by tglx
- Corrected the return value from -EIO to -EPERM when the userspace can't set the affinity

 kernel/irq/proc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/irq/proc.c +7 -3
index 8cccdf40725a..7b3a4c92d148 100644
@@ -142,7 +142,7 @@ static ssize_t write_irq_affinity(int type, struct file *file,
 	int err;
 
 	if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
-		return -EIO;
+		return -EPERM;
 
 	if (!zalloc_cpumask_var(&new_value, GFP_KERNEL))
 		return -ENOMEM;
@@ -340,6 +340,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 	static DEFINE_MUTEX(register_lock);
 	void __maybe_unused *irqp = (void *)(unsigned long) irq;
 	char name [MAX_NAMELEN];
+	umode_t umode = S_IRUGO;
 
 	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
 		return;
@@ -362,8 +363,11 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 		goto out_unlock;
 
 #ifdef CONFIG_SMP
+	if (irq_can_set_affinity_usr(desc->irq_data.irq))
+		umode |= S_IWUSR;
+
 	/* create /proc/irq/<irq>/smp_affinity */
-	proc_create_data("smp_affinity", 0644, desc->dir,
+	proc_create_data("smp_affinity", umode, desc->dir,
 			 &irq_affinity_proc_ops, irqp);
 
 	/* create /proc/irq/<irq>/affinity_hint */
@@ -371,7 +375,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 			irq_affinity_hint_proc_show, irqp);
 
 	/* create /proc/irq/<irq>/smp_affinity_list */
-	proc_create_data("smp_affinity_list", 0644, desc->dir,
+	proc_create_data("smp_affinity_list", umode, desc->dir,
 			 &irq_affinity_list_proc_ops, irqp);
 
 	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
Signature · 1 line
2.34.1
#1 lore raw
Quoted 20 lines · Jeff Xie

The kernel already knows at the time of interrupt allocation that the affinity cannot be controlled by userspace and therefore creating the file with write permissions is wrong.

Therefore set the file permissions to read-only for such interrupts.

---
v2:
- Updated the description suggested by tglx
- Corrected the return value from -EIO to -EPERM when the userspace can't set the affinity

 kernel/irq/proc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/irq/proc.c +0 -0
index 8cccdf40725a..7b3a4c92d148 100644

kernel/irq/proc.c static ssize_t write_irq_affinity(int type, struct file *file, · line 145

Quoted 5 lines
kernel/irq/proc.c +1 -1
@@ -142,7 +142,7 @@ static ssize_t write_irq_affinity(int type, struct file *file,
 	int err;
 
 	if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)
-		return -EIO;
+		return -EPERM;

I drop this hunk as it is unrelated to $subject. That want's to be a separate patch. Documentation/process clearly states:

Solve only one problem per patch.

Thanks,

tglx

#2 lore raw
Quoted 44 lines · Thomas Gleixner
Quoted 41 lines · Jeff Xie

The kernel already knows at the time of interrupt allocation that the

affinity cannot be controlled by userspace and therefore creating the

file with write permissions is wrong.

Therefore set the file permissions to read-only for such interrupts.

---

v2:

- Updated the description suggested by tglx

- Corrected the return value from -EIO to -EPERM when the userspace can't set the affinity

 kernel/irq/proc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/kernel/irq/proc.c b/kernel/irq/proc.c

index 8cccdf40725a..7b3a4c92d148 100644

--- a/kernel/irq/proc.c

+++ b/kernel/irq/proc.c

@@ -142,7 +142,7 @@ static ssize_t write_irq_affinity(int type, struct file *file,

int err;

if (!irq_can_set_affinity_usr(irq) || no_irq_affinity)

- return -EIO;

Quoted 8 lines
Quoted 2 lines

+ return -EPERM;

I drop this hunk as it is unrelated to $subject. That want's to be a

separate patch. Documentation/process clearly states:

Solve only one problem per patch.

Thank you for the reminder. I overlooked that single line change, which is indeed unrelated to the subject. I'll send it as a separate patch.

Quoted 4 lines

Thanks,

tglx

merge bot Applied
#3 lore raw

[tip: irq/core] genirq/procfs: Correctly set file permissions for affinity control files

In reply to Jeff Xie
The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     0b39441eaab8bedcba1129776ec85178d4d0d9fb
Gitweb:        https://git.kernel.org/tip/0b39441eaab8bedcba1129776ec85178d4d0d9fb
Author:        Jeff Xie <[email protected]>
AuthorDate:    Sun, 25 Aug 2024 21:19:11 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 26 Aug 2024 14:00:25 +02:00

genirq/procfs: Correctly set file permissions for affinity control files

The kernel already knows at the time of interrupt allocation whether affinity of an interrupt can be controlled by userspace or not.

It still creates all related procfs control files with read/write permissions. That's inconsistent and non-intuitive for system administrators and tools.

Therefore set the file permissions to read-only for such interrupts.

[ tglx: Massage change log ]

---
 kernel/irq/proc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/irq/proc.c +6 -2
index 8cccdf4..dcf8190 100644
@@ -340,6 +340,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 	static DEFINE_MUTEX(register_lock);
 	void __maybe_unused *irqp = (void *)(unsigned long) irq;
 	char name [MAX_NAMELEN];
+	umode_t umode = S_IRUGO;
 
 	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
 		return;
@@ -362,8 +363,11 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 		goto out_unlock;
 
 #ifdef CONFIG_SMP
+	if (irq_can_set_affinity_usr(desc->irq_data.irq))
+		umode |= S_IWUSR;
+
 	/* create /proc/irq/<irq>/smp_affinity */
-	proc_create_data("smp_affinity", 0644, desc->dir,
+	proc_create_data("smp_affinity", umode, desc->dir,
 			 &irq_affinity_proc_ops, irqp);
 
 	/* create /proc/irq/<irq>/affinity_hint */
@@ -371,7 +375,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 			irq_affinity_hint_proc_show, irqp);
 
 	/* create /proc/irq/<irq>/smp_affinity_list */
-	proc_create_data("smp_affinity_list", 0644, desc->dir,
+	proc_create_data("smp_affinity_list", umode, desc->dir,
 			 &irq_affinity_list_proc_ops, irqp);
 
 	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
#4 lore raw
In reply to Jeff Xie
kernel test robot noticed the following build warnings:

Hi Jeff,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/irq/core]
[also build test WARNING on linus/master v6.11-rc5 next-20240826] [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/Jeff-Xie/genirq-procfs-Make-smp_affinity-read-only-for-interrupts-that-userspace-can-t-set/20240826-153926
base:   tip/irq/core
patch link:    https://lore.kernel.org/r/20240825131911.107119-1-jeff.xie%40linux.dev
patch subject: [PATCH v2] genirq: procfs: Make smp_affinity read-only for interrupts that userspace can't set
config: openrisc-allnoconfig (https://download.01.org/0day-ci/archive/20240827/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240827/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

kernel/irq/proc.c: In function 'register_irq_proc':

>> kernel/irq/proc.c:343:17: warning: unused variable 'umode' [-Wunused-variable]
     343 |         umode_t umode = S_IRUGO;
         |                 ^~~~~

vim +/umode +343 kernel/irq/proc.c

337
338 void register_irq_proc(unsigned int irq, struct irq_desc *desc)
339 {
340 static DEFINE_MUTEX(register_lock);
341 void __maybe_unused *irqp = (void *)(unsigned long) irq;
342 char name [MAX_NAMELEN];

 > 343		umode_t umode = S_IRUGO;

344
345 if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
346 return;
347
348 /*
349 * irq directories are registered only when a handler is
350 * added, not when the descriptor is created, so multiple
351 * tasks might try to register at the same time.
352 */
353 mutex_lock(&register_lock);
354
355 if (desc->dir)
356 goto out_unlock;
357
358 sprintf(name, "%d", irq);
359
360 /* create /proc/irq/1234 */
361 desc->dir = proc_mkdir(name, root_irq_dir);
362 if (!desc->dir)
363 goto out_unlock;
364

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
merge bot Applied
#5 lore raw

[tip: irq/core] genirq/procfs: Correctly set file permissions for affinity control files

In reply to Jeff Xie
The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     413abc4d0f1a1a19713db34ee689609856ffea0e
Gitweb:        https://git.kernel.org/tip/413abc4d0f1a1a19713db34ee689609856ffea0e
Author:        Jeff Xie <[email protected]>
AuthorDate:    Sun, 25 Aug 2024 21:19:11 +08:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 27 Aug 2024 12:16:40 +02:00

genirq/procfs: Correctly set file permissions for affinity control files

The kernel already knows at the time of interrupt allocation whether affinity of an interrupt can be controlled by userspace or not.

It still creates all related procfs control files with read/write permissions. That's inconsistent and non-intuitive for system administrators and tools.

Therefore set the file permissions to read-only for such interrupts.

[ tglx: Massage change log ]

---
 kernel/irq/proc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/irq/proc.c +6 -2
index 8cccdf4..dcf8190 100644
@@ -340,6 +340,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 	static DEFINE_MUTEX(register_lock);
 	void __maybe_unused *irqp = (void *)(unsigned long) irq;
 	char name [MAX_NAMELEN];
+	umode_t umode = S_IRUGO;
 
 	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
 		return;
@@ -362,8 +363,11 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 		goto out_unlock;
 
 #ifdef CONFIG_SMP
+	if (irq_can_set_affinity_usr(desc->irq_data.irq))
+		umode |= S_IWUSR;
+
 	/* create /proc/irq/<irq>/smp_affinity */
-	proc_create_data("smp_affinity", 0644, desc->dir,
+	proc_create_data("smp_affinity", umode, desc->dir,
 			 &irq_affinity_proc_ops, irqp);
 
 	/* create /proc/irq/<irq>/affinity_hint */
@@ -371,7 +375,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 			irq_affinity_hint_proc_show, irqp);
 
 	/* create /proc/irq/<irq>/smp_affinity_list */
-	proc_create_data("smp_affinity_list", 0644, desc->dir,
+	proc_create_data("smp_affinity_list", umode, desc->dir,
 			 &irq_affinity_list_proc_ops, irqp);
 
 	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
merge bot Applied
#6 lore raw

[tip: irq/core] genirq/procfs: Correctly set file permissions for affinity control files

In reply to Jeff Xie
The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     92f9d825b12fa3f6c14b42405489880d0694c96f
Gitweb:        https://git.kernel.org/tip/92f9d825b12fa3f6c14b42405489880d0694c96f
Author:        Jeff Xie <[email protected]>
AuthorDate:    Sun, 25 Aug 2024 21:19:11 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 27 Aug 2024 13:54:15 +02:00

genirq/procfs: Correctly set file permissions for affinity control files

The kernel already knows at the time of interrupt allocation whether affinity of an interrupt can be controlled by userspace or not.

It still creates all related procfs control files with read/write permissions. That's inconsistent and non-intuitive for system administrators and tools.

Therefore set the file permissions to read-only for such interrupts.

[ tglx: Massage change log ]

---
 kernel/irq/proc.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/irq/proc.c +6 -2
index 8cccdf4..dcf8190 100644
@@ -340,6 +340,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 	static DEFINE_MUTEX(register_lock);
 	void __maybe_unused *irqp = (void *)(unsigned long) irq;
 	char name [MAX_NAMELEN];
+	umode_t umode = S_IRUGO;
 
 	if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip))
 		return;
@@ -362,8 +363,11 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 		goto out_unlock;
 
 #ifdef CONFIG_SMP
+	if (irq_can_set_affinity_usr(desc->irq_data.irq))
+		umode |= S_IWUSR;
+
 	/* create /proc/irq/<irq>/smp_affinity */
-	proc_create_data("smp_affinity", 0644, desc->dir,
+	proc_create_data("smp_affinity", umode, desc->dir,
 			 &irq_affinity_proc_ops, irqp);
 
 	/* create /proc/irq/<irq>/affinity_hint */
@@ -371,7 +375,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 			irq_affinity_hint_proc_show, irqp);
 
 	/* create /proc/irq/<irq>/smp_affinity_list */
-	proc_create_data("smp_affinity_list", 0644, desc->dir,
+	proc_create_data("smp_affinity_list", umode, desc->dir,
 			 &irq_affinity_list_proc_ops, irqp);
 
 	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,
merge bot Applied
#7 lore raw

[tip: irq/core] genirq/proc: Correctly set file permissions for affinity control files

In reply to Jeff Xie
The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     c7718e5c76d49b5bb394265383ae51f766d5dd3a
Gitweb:        https://git.kernel.org/tip/c7718e5c76d49b5bb394265383ae51f766d5dd3a
Author:        Jeff Xie <[email protected]>
AuthorDate:    Sun, 25 Aug 2024 21:19:11 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 29 Aug 2024 16:41:42 +02:00

genirq/proc: Correctly set file permissions for affinity control files

The kernel already knows at the time of interrupt allocation whether affinity of an interrupt can be controlled by userspace or not.

It still creates all related procfs control files with read/write permissions. That's inconsistent and non-intuitive for system administrators and tools.

Therefore set the file permissions to read-only for such interrupts.

[ tglx: Massage change log, fixed UP build ]

---
 kernel/irq/proc.c |  9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/irq/proc.c +7 -2
index 8cccdf4..9b3b12a 100644
@@ -362,8 +362,13 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 		goto out_unlock;
 
 #ifdef CONFIG_SMP
+	umode_t umode = S_IRUGO;
+
+	if (irq_can_set_affinity_usr(desc->irq_data.irq))
+		umode |= S_IWUSR;
+
 	/* create /proc/irq/<irq>/smp_affinity */
-	proc_create_data("smp_affinity", 0644, desc->dir,
+	proc_create_data("smp_affinity", umode, desc->dir,
 			 &irq_affinity_proc_ops, irqp);
 
 	/* create /proc/irq/<irq>/affinity_hint */
@@ -371,7 +376,7 @@ void register_irq_proc(unsigned int irq, struct irq_desc *desc)
 			irq_affinity_hint_proc_show, irqp);
 
 	/* create /proc/irq/<irq>/smp_affinity_list */
-	proc_create_data("smp_affinity_list", 0644, desc->dir,
+	proc_create_data("smp_affinity_list", umode, desc->dir,
 			 &irq_affinity_list_proc_ops, irqp);
 
 	proc_create_single_data("node", 0444, desc->dir, irq_node_proc_show,