mirror of
https://github.com/torvalds/linux.git
synced 2024-11-11 22:51:42 +00:00
bpf: Replace strncpy() with strscpy()
Using strncpy() on NUL-terminated strings is considered deprecated[1]. Moreover, if the length of 'task->comm' is less than the destination buffer size, strncpy() will NUL-pad the destination buffer, which is a needless performance penalty. Replacing strncpy() with strscpy() fixes all these issues. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings Signed-off-by: Yuntao Wang <ytcoode@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20220304070408.233658-1-ytcoode@gmail.com
This commit is contained in:
parent
9c6e6a80ee
commit
03b9c7fa3f
@ -225,13 +225,8 @@ BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
|
|||||||
if (unlikely(!task))
|
if (unlikely(!task))
|
||||||
goto err_clear;
|
goto err_clear;
|
||||||
|
|
||||||
strncpy(buf, task->comm, size);
|
/* Verifier guarantees that size > 0 */
|
||||||
|
strscpy(buf, task->comm, size);
|
||||||
/* Verifier guarantees that size > 0. For task->comm exceeding
|
|
||||||
* size, guarantee that buf is %NUL-terminated. Unconditionally
|
|
||||||
* done here to save the size test.
|
|
||||||
*/
|
|
||||||
buf[size - 1] = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
err_clear:
|
err_clear:
|
||||||
memset(buf, 0, size);
|
memset(buf, 0, size);
|
||||||
|
Loading…
Reference in New Issue
Block a user