Commit d4966c1
bpftool: Fix bypass of the batch line length check by comments
do_batch() strips trailing comments by truncating the line at '#'
before checking whether fgets() filled the buffer. If a batch line
longer than the buffer contains a '#' within the first
sizeof(buf) - 1 bytes, the truncation makes strlen(buf) smaller and the
line-length check is bypassed. The unread remainder of the line then
stays in the file stream and is parsed and executed as a separate
command on the next loop iteration.
Continuation lines handled below are affected the same way: an overlong
continuation line containing '#' bypasses the "command is too
long" check, and its unread remainder is executed as a separate command.
Detect the truncated read before stripping the comment, using memchr()
to look for a newline (instead of strlen(), which is also fooled by an
embedded NUL byte) and feof() to tell an overlong line apart from a
final line without a trailing newline. A line that fills the buffer
exactly (the byte after the read is a newline) is not treated as
truncated, so valid maximal-length lines are no longer rejected. Use the
result for the line-length checks, so overlong lines are rejected
regardless of comments or NUL bytes.
Fixes: 71bb428 ("tools: bpf: add bpftool")
Signed-off-by: Yuan Chen <chenyuan@kylinos.cn>1 parent 0210de9 commit d4966c1
1 file changed
Lines changed: 24 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
370 | 386 | | |
371 | 387 | | |
372 | 388 | | |
373 | 389 | | |
374 | | - | |
| 390 | + | |
375 | 391 | | |
376 | 392 | | |
377 | 393 | | |
| |||
380 | 396 | | |
381 | 397 | | |
382 | 398 | | |
| 399 | + | |
| 400 | + | |
383 | 401 | | |
384 | 402 | | |
385 | 403 | | |
| |||
388 | 406 | | |
389 | 407 | | |
390 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
391 | 412 | | |
392 | 413 | | |
393 | 414 | | |
394 | 415 | | |
395 | | - | |
| 416 | + | |
| 417 | + | |
396 | 418 | | |
397 | 419 | | |
398 | 420 | | |
| |||
0 commit comments