Skip to content

Commit c73c935

Browse files
ytcoodeNobody
authored andcommitted
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 <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent 304e22c commit c73c935

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

kernel/bpf/helpers.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,8 @@ BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size)
225225
if (unlikely(!task))
226226
goto err_clear;
227227

228-
strncpy(buf, task->comm, size);
229-
230-
/* Verifier guarantees that size > 0. For task->comm exceeding
231-
* size, guarantee that buf is %NUL-terminated. Unconditionally
232-
* done here to save the size test.
233-
*/
234-
buf[size - 1] = 0;
228+
/* Verifier guarantees that size > 0 */
229+
strscpy(buf, task->comm, size);
235230
return 0;
236231
err_clear:
237232
memset(buf, 0, size);

0 commit comments

Comments
 (0)