kernel: Limit parsecmd() to a maximum of READSTR bytes

A user can create a large demand paged segment
and then do write to a ctl file with a very large buffer
driving the kernel into an out-of-memory condition.

For all practcal purposes, limit the input buffer size
to something reasonable. READSTR is 8000 bytes, which
would be enougth for even the largest ctl messages.
This commit is contained in:
cinap_lenrek 2024-10-20 12:07:08 +00:00
parent 5beba2cf33
commit fcdfb151e2
2 changed files with 12 additions and 1 deletions

View file

@ -67,6 +67,14 @@ is allocated by
.IR malloc (9)),
and the caller is responsible for freeing it using
.IR free .
To prevent denial of service to the kernel,
.I parsecmd
will error out if
.I n
exceeds
.B READSTR
bytes.
.PP
.I Cmderror
prepends the given format with the original command,
then calls

View file

@ -36,10 +36,13 @@ ncmdfield(char *p, int n)
Cmdbuf*
parsecmd(char *p, int n)
{
Cmdbuf *volatile cb;
Cmdbuf *cb;
int nf;
char *sp;
if(up!=nil && (uint)n > READSTR)
error("control message too big");
nf = ncmdfield(p, n);
/* allocate Cmdbuf plus string pointers plus copy of string including \0 */