security/selinux/selinuxfs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
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",
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
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
>
>
>
>
© 2016 - 2025 Red Hat, Inc.