Created attachment 237614 [details] provoke a crash in netgraph ng_parse_composite() In netgraph/ng_parse.c's ng_parse_composite(): const int num = ng_get_composite_len(type, start, buf, ctype); ...; foff = malloc(num * sizeof(*foff), M_NETGRAPH_PARSE, M_NOWAIT | M_ZERO); ng_get_composite_len() reads num from the message, and it's used without a sanity check. If it's negative, malloc() either page faults or (with INVARIANTS) fails MPASS(size > 0). I've attached a demo that triggers the crash with an NGM_BINARY2ASCII NGM_LISTNAMES control message: # cc ng12a.c -lnetgraph # ./a.out panic: Fatal page fault at 0xffffffc000340f48: 0x27fffd000807460 panic() at panic+0x2a page_fault_handler() at page_fault_handler+0x1d6 do_trap_supervisor() at do_trap_supervisor+0x74 cpu_exception_handler_supervisor() at cpu_exception_handler_supervisor+0x70 --- exception 13, tval = 0x27fffd000807460 vmem_alloc() at vmem_alloc+0x76 kmem_malloc_domain() at kmem_malloc_domain+0x52 kmem_malloc_domainset() at kmem_malloc_domainset+0x36 malloc_large() at malloc_large+0x2a malloc() at malloc+0xf8 ng_parse_composite() at ng_parse_composite+0x64 ng_array_getDefault() at ng_array_getDefault+0x24 ng_get_composite_elem_default() at ng_get_composite_elem_default+0x74 ng_unparse_composite() at ng_unparse_composite+0x17e ng_struct_unparse() at ng_struct_unparse+0x42 ng_unparse() at ng_unparse+0x30 ng_generic_msg() at ng_generic_msg+0x96e ng_apply_item() at ng_apply_item+0xf6 ng_snd_item() at ng_snd_item+0x1bc ngc_send() at ngc_send+0x260 sosend_generic() at sosend_generic+0x384 sosend() at sosend+0x68 kern_sendit() at kern_sendit+0x170 sendit() at sendit+0x9c sys_sendto() at sys_sendto+0x40 syscallenter() at syscallenter+0xec ecall_handler() at ecall_handler+0x18 do_trap_user() at do_trap_user+0xf6 cpu_exception_handler_user() at cpu_exception_handler_user+0x72 --- syscall (133, FreeBSD ELF64, sys_sendto)
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=ae4f39464c61182c2bdfd3cc61594f8707b3daf0 commit ae4f39464c61182c2bdfd3cc61594f8707b3daf0 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2022-11-01 14:01:29 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2024-11-21 20:53:04 +0000 ng_parse: disallow negative length for malloc This is an interim robustness improvement; further improvements as described in the PR and/or Phabricator review are still needed. PR: 267334 Reported by: Robert Morris <rtm@lcs.mit.edu> Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D37229 sys/netgraph/ng_parse.c | 2 ++ 1 file changed, 2 insertions(+)
I've committed an interim improvement. Gleb raises valid points in the linked Phabricator review, so don't consider this issue closed.
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=0e0329c6dbabc12f5ab08bb63929a2ba6d506084 commit 0e0329c6dbabc12f5ab08bb63929a2ba6d506084 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2022-11-01 14:01:29 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2024-12-02 21:10:31 +0000 ng_parse: disallow negative length for malloc This is an interim robustness improvement; further improvements as described in the PR and/or Phabricator review are still needed. PR: 267334 Reported by: Robert Morris <rtm@lcs.mit.edu> Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D37229 (cherry picked from commit ae4f39464c61182c2bdfd3cc61594f8707b3daf0) sys/netgraph/ng_parse.c | 2 ++ 1 file changed, 2 insertions(+)
^Triage: to committer: can this PR be closed now?
(In reply to Mark Linimon from comment #4) > ^Triage: to committer: can this PR be closed now? My commit is a small robustness improvement, but there are more significant changes we should look at that are described in https://reviews.freebsd.org/D37229.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=375527545c85362f14070d35575f9bcd7092f4b9 commit 375527545c85362f14070d35575f9bcd7092f4b9 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2025-08-25 14:25:13 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2025-09-01 21:12:45 +0000 ng_parse: Add upper bound to avoid possible overflow Also move num initialization for clarity. We still need to check num in ng_unparse_composite (reported by des@ in D52151) but this is another incremental improvement in netgraph input validation. Reviewed by: des PR: 267334 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D52151 sys/netgraph/ng_parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
ng_parse_composite should be good enough now to close this after MFC. Someone needs to review the rest of ng_parse.c though as discussed in D52151.
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=4407e10fdc8b969775233d47c05559c2601b60f4 commit 4407e10fdc8b969775233d47c05559c2601b60f4 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2025-08-25 14:25:13 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2025-09-04 12:08:51 +0000 ng_parse: Add upper bound to avoid possible overflow Also move num initialization for clarity. We still need to check num in ng_unparse_composite (reported by des@ in D52151) but this is another incremental improvement in netgraph input validation. Reviewed by: des PR: 267334 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D52151 (cherry picked from commit 375527545c85362f14070d35575f9bcd7092f4b9) sys/netgraph/ng_parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=e3af3a23c155d85275c6e32f2fe943468f252ba3 commit e3af3a23c155d85275c6e32f2fe943468f252ba3 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2025-08-25 14:25:13 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2025-09-04 13:05:30 +0000 ng_parse: Add upper bound to avoid possible overflow Also move num initialization for clarity. We still need to check num in ng_unparse_composite (reported by des@ in D52151) but this is another incremental improvement in netgraph input validation. Reviewed by: des PR: 267334 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D52151 (cherry picked from commit 375527545c85362f14070d35575f9bcd7092f4b9) (cherry picked from commit 4407e10fdc8b969775233d47c05559c2601b60f4) sys/netgraph/ng_parse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=bc77917cd053f5ce3d8b5e9ad6b64bad69a7bd06 commit bc77917cd053f5ce3d8b5e9ad6b64bad69a7bd06 Author: Ed Maste <emaste@FreeBSD.org> AuthorDate: 2022-11-01 14:01:29 +0000 Commit: Ed Maste <emaste@FreeBSD.org> CommitDate: 2025-09-04 13:05:30 +0000 ng_parse: disallow negative length for malloc This is an interim robustness improvement; further improvements as described in the PR and/or Phabricator review are still needed. PR: 267334 Reported by: Robert Morris <rtm@lcs.mit.edu> Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D37229 (cherry picked from commit ae4f39464c61182c2bdfd3cc61594f8707b3daf0) (cherry picked from commit 0e0329c6dbabc12f5ab08bb63929a2ba6d506084) sys/netgraph/ng_parse.c | 2 ++ 1 file changed, 2 insertions(+)