lockdown: avoid extra call to strlen() in lockdown_read()
Hi Dmitry,
Quoted 31 lines
Since s*printf() family of functions returns the number of characters emitted, avoid redundant call to
strlen()inlockdown_read()and prefersnprintf()oversprintf()for an extra protection against buffer overflow.Signed-off-by Dmitry Antipov <[email protected]>--- security/lockdown/lockdown.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)security/lockdown/lockdown.c +6
-2@@ -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); } }
security/lockdown/lockdown.c static ssize_t lockdown_read(struct file *filp, char · line 124
Quoted 6 lines
security/lockdown/lockdown.c +1
-1@@ -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
Quoted 5 lines
security/lockdown/lockdown.c +0
-0}static
ssize_tlockdown_write(struct file *file, const char__user*buf,
--
2.50.1