* [PATCH] lockdown: avoid extra call to strlen() in lockdown_read()
@ 2025-08-14 14:00 Dmitry Antipov
0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Antipov @ 2025-08-14 14:00 UTC (permalink / raw)
To: Nicolas Bouchinet, Xiu Jianfeng, Paul Moore, James Morris,
Serge E . Hallyn
Cc: linux-security-module, Dmitry Antipov
Since s*printf() family of functions returns the number of characters
emitted, avoid redundant call to strlen() in lockdown_read() and prefer
snprintf() over sprintf() for an extra protection against buffer overflow.
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
security/lockdown/lockdown.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c
index cf83afa1d879..10537d7c4437 100644
--- a/security/lockdown/lockdown.c
+++ b/security/lockdown/lockdown.c
@@ -106,9 +106,13 @@ static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
const char *label = lockdown_reasons[level];
if (kernel_locked_down == level)
- offset += sprintf(temp+offset, "[%s] ", label);
+ offset += snprintf(temp + offset,
+ sizeof(temp) - offset,
+ "[%s] ", label);
else
- offset += sprintf(temp+offset, "%s ", label);
+ offset += snprintf(temp + offset,
+ sizeof(temp) - offset,
+ "%s ", label);
}
}
@@ -116,7 +120,7 @@ static ssize_t lockdown_read(struct file *filp, char __user *buf, size_t count,
if (offset > 0)
temp[offset-1] = '\n';
- return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
+ return simple_read_from_buffer(buf, count, ppos, temp, offset);
}
static ssize_t lockdown_write(struct file *file, const char __user *buf,
--
2.50.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] lockdown: avoid extra call to strlen() in lockdown_read()
@ 2025-08-15 12:28 Xiujianfeng
0 siblings, 0 replies; 2+ messages in thread
From: Xiujianfeng @ 2025-08-15 12:28 UTC (permalink / raw)
To: Dmitry Antipov, Nicolas Bouchinet, Paul Moore, James Morris,
Serge E . Hallyn
Cc: linux-security-module@vger.kernel.org
Hi Dmitry,
> Since s*printf() family of functions returns the number of characters emitted,
> avoid redundant call to strlen() in lockdown_read() and prefer
> snprintf() over sprintf() for an extra protection against buffer overflow.
>
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
> security/lockdown/lockdown.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/security/lockdown/lockdown.c b/security/lockdown/lockdown.c
> index cf83afa1d879..10537d7c4437 100644
> --- a/security/lockdown/lockdown.c
> +++ b/security/lockdown/lockdown.c
> @@ -106,9 +106,13 @@ static ssize_t lockdown_read(struct file *filp, char
> __user *buf, size_t count,
> const char *label = lockdown_reasons[level];
>
> if (kernel_locked_down == level)
> - offset += sprintf(temp+offset, "[%s] ", label);
> + offset += snprintf(temp + offset,
> + sizeof(temp) - offset,
> + "[%s] ", label);
> else
> - offset += sprintf(temp+offset, "%s ", label);
> + offset += snprintf(temp + offset,
> + sizeof(temp) - offset,
> + "%s ", label);
> }
> }
>
> @@ -116,7 +120,7 @@ static ssize_t lockdown_read(struct file *filp, char
> __user *buf, size_t count,
> if (offset > 0)
> temp[offset-1] = '\n';
>
> - return simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
> + return simple_read_from_buffer(buf, count, ppos, temp, offset);
Thanks for your patch.
Since the current `lockdown_levels` array is static and has only three members,
and the total number of characters of these three reasons is far from exceeding 80,
there seems to be no risk of buffer overflow for now.
About the change to strlen, I think lockdown_read() is not on the hot path, the impact
is minimal.
So I prefer to leave them as is, thanks.
Best regards,
Xiu
> }
>
> static ssize_t lockdown_write(struct file *file, const char __user *buf,
> --
> 2.50.1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-08-15 12:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-15 12:28 [PATCH] lockdown: avoid extra call to strlen() in lockdown_read() Xiujianfeng
-- strict thread matches above, loose matches on Subject: below --
2025-08-14 14:00 Dmitry Antipov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).