diff mbox series

[linux-next] selinux: use sysfs_emit() instead of scnprintf()

Message ID 20250315141136817waFGT5DFhPs9QMwybNwb5@zte.com.cn (mailing list archive)
State Rejected
Delegated to: Paul Moore
Headers show
Series [linux-next] selinux: use sysfs_emit() instead of scnprintf() | expand

Commit Message

xie.ludan@zte.com.cn March 15, 2025, 6:11 a.m. UTC
From: XieLudan <xie.ludan@zte.com.cn>

Follow the advice in Documentation/filesystems/sysfs.rst:
show() should only use sysfs_emit() or sysfs_emit_at() when formatting
the value to be returned to user space.

Signed-off-by: XieLudan <xie.ludan@zte.com.cn>
---
 security/selinux/selinuxfs.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

Comments

Christian Göttsche March 16, 2025, 1:38 p.m. UTC | #1
On Sat, 15 Mar 2025 at 07:11, <xie.ludan@zte.com.cn> wrote:
>
> From: XieLudan <xie.ludan@zte.com.cn>
>
>
> Follow the advice in Documentation/filesystems/sysfs.rst:
>
> show() should only use sysfs_emit() or sysfs_emit_at() when formatting
>
> the value to be returned to user space.
>
>
> Signed-off-by: XieLudan <xie.ludan@zte.com.cn>
>
> ---
>
>  security/selinux/selinuxfs.c | 20 ++++++++++----------
>
>  1 file changed, 10 insertions(+), 10 deletions(-)
>
>
> diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c
>
> index 47480eb2189b..17c56fc87d98 100644
>
> --- a/security/selinux/selinuxfs.c
>
> +++ b/security/selinux/selinuxfs.c
>
> @@ -126,7 +126,7 @@ static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
>
>   char tmpbuf[TMPBUFLEN];
>
>   ssize_t length;
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
>
> + length = sysfs_emit(tmpbuf, "%d",

That would be dangerous since the target buffer is of size TMPBUFLEN
(12) and not PAGE_SIZE (4096).

>      enforcing_enabled());
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
> @@ -206,7 +206,7 @@ static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
>
>   security_get_reject_unknown() :
>
>   !security_get_allow_unknown();
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
>
> + length = sysfs_emit(tmpbuf, "%d", handle_unknown);
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
>
>
> @@ -314,7 +314,7 @@ static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
>
>   char tmpbuf[TMPBUFLEN];
>
>   ssize_t length;
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
>
> + length = sysfs_emit(tmpbuf, "%u", POLICYDB_VERSION_MAX);
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
>
>
> @@ -345,7 +345,7 @@ static ssize_t sel_read_mls(struct file *filp, char __user *buf,
>
>   char tmpbuf[TMPBUFLEN];
>
>   ssize_t length;
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
>
> + length = sysfs_emit(tmpbuf, "%d",
>
>      security_mls_enabled());
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
> @@ -670,7 +670,7 @@ static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
>
>   char tmpbuf[TMPBUFLEN];
>
>   ssize_t length;
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
>
> + length = sysfs_emit(tmpbuf, "%u",
>
>      checkreqprot_get());
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
> @@ -1226,7 +1226,7 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,
>
>   ret = cur_enforcing;
>
>   goto out_unlock;
>
>   }
>
> - length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
>
> + length = sysfs_emit(page, "%d %d", cur_enforcing,
>
>     fsi->bool_pending_values[index]);
>
>   mutex_unlock(&selinux_state.policy_mutex);
>
>   ret = simple_read_from_buffer(buf, count, ppos, page, length);
>
> @@ -1416,7 +1416,7 @@ static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
>
>   char tmpbuf[TMPBUFLEN];
>
>   ssize_t length;
>
>
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
>
> + length = sysfs_emit(tmpbuf, "%u",
>
>      avc_get_cache_threshold());
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
> @@ -1726,7 +1726,7 @@ static ssize_t sel_read_class(struct file *file, char __user *buf,
>
>  {
>
>   unsigned long ino = file_inode(file)->i_ino;
>
>   char res[TMPBUFLEN];
>
> - ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
>
> + ssize_t len = sysfs_emit(res, "%d", sel_ino_to_class(ino));
>
>   return simple_read_from_buffer(buf, count, ppos, res, len);
>
>  }
>
>
>
> @@ -1740,7 +1740,7 @@ static ssize_t sel_read_perm(struct file *file, char __user *buf,
>
>  {
>
>   unsigned long ino = file_inode(file)->i_ino;
>
>   char res[TMPBUFLEN];
>
> - ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
>
> + ssize_t len = sysfs_emit(res, "%d", sel_ino_to_perm(ino));
>
>   return simple_read_from_buffer(buf, count, ppos, res, len);
>
>  }
>
>
>
> @@ -1758,7 +1758,7 @@ static ssize_t sel_read_policycap(struct file *file, char __user *buf,
>
>   unsigned long i_ino = file_inode(file)->i_ino;
>
>
>
>   value = security_policycap_supported(i_ino & SEL_INO_MASK);
>
> - length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
>
> + length = sysfs_emit(tmpbuf, "%d", value);
>
>
>
>   return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
>
>  }
>
> --
>
> 2.25.1
>
>
>
>
diff mbox series

Patch

diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c
index 47480eb2189b..17c56fc87d98 100644
--- a/security/selinux/selinuxfs.c
+++ b/security/selinux/selinuxfs.c
@@ -126,7 +126,7 @@  static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
 	char tmpbuf[TMPBUFLEN];
 	ssize_t length;
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
+	length = sysfs_emit(tmpbuf, "%d",
 			   enforcing_enabled());
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
@@ -206,7 +206,7 @@  static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
 		security_get_reject_unknown() :
 		!security_get_allow_unknown();
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
+	length = sysfs_emit(tmpbuf, "%d", handle_unknown);
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
 
@@ -314,7 +314,7 @@  static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
 	char tmpbuf[TMPBUFLEN];
 	ssize_t length;
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
+	length = sysfs_emit(tmpbuf, "%u", POLICYDB_VERSION_MAX);
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
 
@@ -345,7 +345,7 @@  static ssize_t sel_read_mls(struct file *filp, char __user *buf,
 	char tmpbuf[TMPBUFLEN];
 	ssize_t length;
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
+	length = sysfs_emit(tmpbuf, "%d",
 			   security_mls_enabled());
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
@@ -670,7 +670,7 @@  static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
 	char tmpbuf[TMPBUFLEN];
 	ssize_t length;
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
+	length = sysfs_emit(tmpbuf, "%u",
 			   checkreqprot_get());
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
@@ -1226,7 +1226,7 @@  static ssize_t sel_read_bool(struct file *filep, char __user *buf,
 		ret = cur_enforcing;
 		goto out_unlock;
 	}
-	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
+	length = sysfs_emit(page, "%d %d", cur_enforcing,
 			  fsi->bool_pending_values[index]);
 	mutex_unlock(&selinux_state.policy_mutex);
 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
@@ -1416,7 +1416,7 @@  static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
 	char tmpbuf[TMPBUFLEN];
 	ssize_t length;
 
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
+	length = sysfs_emit(tmpbuf, "%u",
 			   avc_get_cache_threshold());
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }
@@ -1726,7 +1726,7 @@  static ssize_t sel_read_class(struct file *file, char __user *buf,
 {
 	unsigned long ino = file_inode(file)->i_ino;
 	char res[TMPBUFLEN];
-	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
+	ssize_t len = sysfs_emit(res, "%d", sel_ino_to_class(ino));
 	return simple_read_from_buffer(buf, count, ppos, res, len);
 }
 
@@ -1740,7 +1740,7 @@  static ssize_t sel_read_perm(struct file *file, char __user *buf,
 {
 	unsigned long ino = file_inode(file)->i_ino;
 	char res[TMPBUFLEN];
-	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
+	ssize_t len = sysfs_emit(res, "%d", sel_ino_to_perm(ino));
 	return simple_read_from_buffer(buf, count, ppos, res, len);
 }
 
@@ -1758,7 +1758,7 @@  static ssize_t sel_read_policycap(struct file *file, char __user *buf,
 	unsigned long i_ino = file_inode(file)->i_ino;
 
 	value = security_policycap_supported(i_ino & SEL_INO_MASK);
-	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
+	length = sysfs_emit(tmpbuf, "%d", value);
 
 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
 }