kmsg: Use vmalloc instead of kmalloc when writing
PATCH 1/1 kmsg: Use vmalloc instead of kmalloc when writing
There are no size checks in kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.
One option would be to limit it to something, but we can't come up with a number that would make sense.
Instead, just use vmalloc so that nothing would break with large amounts of data.
--- drivers/char/mem.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/char/mem.c +2 -2
@@ -815,7 +815,7 @@ static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,ssize_t ret = -EFAULT ;size_t len =iov_length (iv, count); - line =kmalloc (len +1 ,GFP_KERNEL ); + line =vmalloc (len +1 );if (line ==NULL )return -ENOMEM ;
@@ -836,7 +836,7 @@ static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,if (ret > len) ret = len; out: -kfree (line); +vfree (line);return ret; }
Signature · 1 line
1.7.8.4
Greg KH
maintainerQuoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.
Really? Have you seen this fail? As only root can do this, is this really a problem?
Quoted 5 lines
One option would be to limit it to something, but we can't come up with a number that would make sense.
Instead, just use vmalloc so that nothing would break with large amounts of data.
Are you sure this will work properly? Have you tested it with large amounts of data?
thanks,
greg k-h
Quoted 7 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.Really? Have you seen this fail? As only root can do this, is this really a problem?
Only root, and a whole bunch of management software that dumps data into /dev/kmsg (systemd and friends).
Quoted 8 lines
Quoted 5 lines
One option would be to limit it to something, but we can't come up with a number that would make sense.
Instead, just use vmalloc so that nothing would break with large amounts of data.
Are you sure this will work properly? Have you tested it with large amounts of data?
My test was dumping 800mb of data into it, if there's anything else I should try please let me know.
Thanks.
Greg KH
maintainerQuoted 11 lines · Sasha Levin
Quoted 7 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.Really? Have you seen this fail? As only root can do this, is this really a problem?
Only root, and a whole bunch of management software that dumps data into
/dev/kmsg(systemd and friends).
Running as root, do any of these cause problems by asking for too much memory here? Is this something that needs to be addressed now, and in stable kernels, or can it wait for 3.5?
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
One option would be to limit it to something, but we can't come up with a number that would make sense.
Instead, just use vmalloc so that nothing would break with large amounts of data.
Are you sure this will work properly? Have you tested it with large amounts of data?
My test was dumping 800mb of data into it, if there's anything else I should try please let me know.
Did that fail before and now it works properly?
thanks,
greg k-h
Quoted 10 lines · Greg KH
Quoted 9 lines · Sasha Levin
Quoted 7 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.Really? Have you seen this fail? As only root can do this, is this really a problem?
Quoted 6 lines
Quoted 2 lines
Only root, and a whole bunch of management software that dumps data into
/dev/kmsg(systemd and friends).Running as root, do any of these cause problems by asking for too much memory here? Is this something that needs to be addressed now, and in stable kernels, or can it wait for 3.5?
The only harm there is a kernel warning and the failure to write that specific message to kmsg, combined with the fact that no one complained about it before me I think it can probably wait for 3.5.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
One option would be to limit it to something, but we can't come up with a number that would make sense.
Instead, just use vmalloc so that nothing would break with large amounts of data.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Are you sure this will work properly? Have you tested it with large amounts of data?
My test was dumping 800mb of data into it, if there's anything else I should try please let me know.
Did that fail before and now it works properly?
That's correct.
Quoted 7 lines · Greg KH
Quoted 6 lines · Sasha Levin
Quoted 5 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Really? Have you seen this fail? As only root can do this, is this really a problem?
Only root, and a whole bunch of management software that dumps data into
/dev/kmsg(systemd and friends).Running as root, do any of these cause problems by asking for too much memory here?
Running as root is not a guarantee for correctness. So the syscall should cope with bogus requests from user space and not rely on the sanity of anything. Looking at the main users which polute dmesg I'm inclined to assume insanity in the first place.
As Sasha pointed out there is either the variant to use vmalloc and grant any write size or limit the size to something sensible. Though given the users of this, coming up with something sensible might be a problem.
Quoted 2 lines
Is this something that needs to be addressed now, and in stable kernels, or can it wait for 3.5?
Yes, it want's to be addressed now and it want's to be in stable as well. syscalls which have no bound checking are evil, no matter what.
Thanks,
tglx
Greg KH
maintainerQuoted 27 lines · Thomas Gleixner
Quoted 7 lines · Greg KH
Quoted 6 lines · Sasha Levin
Quoted 5 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Really? Have you seen this fail? As only root can do this, is this really a problem?
Only root, and a whole bunch of management software that dumps data into
/dev/kmsg(systemd and friends).Running as root, do any of these cause problems by asking for too much memory here?
Running as root is not a guarantee for correctness. So the syscall should cope with bogus requests from user space and not rely on the sanity of anything. Looking at the main users which polute dmesg I'm inclined to assume insanity in the first place.
As Sasha pointed out there is either the variant to use vmalloc and grant any write size or limit the size to something sensible. Though given the users of this, coming up with something sensible might be a problem.
Quoted 5 lines
Quoted 2 lines
Is this something that needs to be addressed now, and in stable kernels, or can it wait for 3.5?
Yes, it want's to be addressed now and it want's to be in stable as well. syscalls which have no bound checking are evil, no matter what.
So, should we cap the size at something "super large" then as well?
thanks,
greg k-h
Quoted 28 lines · Greg KH
Quoted 27 lines · Thomas Gleixner
Quoted 7 lines · Greg KH
Quoted 6 lines · Sasha Levin
Quoted 5 lines · Greg KH
Quoted 3 lines · Sasha Levin
There are no size checks in
kmsg_write(), and we try allocating enough memory to store everything userspace gave us, which may be too much for kmalloc to allocate.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Really? Have you seen this fail? As only root can do this, is this really a problem?
Only root, and a whole bunch of management software that dumps data into
/dev/kmsg(systemd and friends).Running as root, do any of these cause problems by asking for too much memory here?
Running as root is not a guarantee for correctness. So the syscall should cope with bogus requests from user space and not rely on the sanity of anything. Looking at the main users which polute dmesg I'm inclined to assume insanity in the first place.
As Sasha pointed out there is either the variant to use vmalloc and grant any write size or limit the size to something sensible. Though given the users of this, coming up with something sensible might be a problem.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Is this something that needs to be addressed now, and in stable kernels, or can it wait for 3.5?
Yes, it want's to be addressed now and it want's to be in stable as well. syscalls which have no bound checking are evil, no matter what.
So, should we cap the size at something "super large" then as well?
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
Thanks,
tglx
Quoted 8 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
I guess a page would be a reasonable size, similar to what we do for sysfs.
Fine with me.
Greg KH
maintainerQuoted 8 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
I guess a page would be a reasonable size, similar to what we do for sysfs.
Ok. Sasha, as you seem to have noticed this, care to dig in syslog and systemd to get an idea of the buffer sizes they are expecting to pass into kmsg, and if they can handle a short write properly? If so, restricting it to a page is fine with me, otherwise we might want to make it a bit bigger.
thanks,
greg k-h
Quoted 7 lines · Greg KH
Quoted 6 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
Quoted 8 lines
Quoted 2 lines
I guess a page would be a reasonable size, similar to what we do for sysfs.
Ok. Sasha, as you seem to have noticed this, care to dig in syslog and systemd to get an idea of the buffer sizes they are expecting to pass into kmsg, and if they can handle a short write properly? If so, restricting it to a page is fine with me, otherwise we might want to make it a bit bigger.
systemd seems to use posix LINE_MAX sized buffers, syslog-ng uses dynamic strings, but it chews them one line at the time.
Arnd Bergmann
maintainerQuoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
I guess a page would be a reasonable size, similar to what we do for sysfs.
Arnd
Greg KH
maintainerQuoted 11 lines · Sasha Levin
Quoted 10 lines · Greg KH
Quoted 8 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
I guess a page would be a reasonable size, similar to what we do for sysfs.
Quoted 8 lines
Quoted 5 lines
Ok. Sasha, as you seem to have noticed this, care to dig in syslog and systemd to get an idea of the buffer sizes they are expecting to pass into kmsg, and if they can handle a short write properly? If so, restricting it to a page is fine with me, otherwise we might want to make it a bit bigger.
systemd seems to use posix
LINE_MAXsized buffers, syslog-ng uses dynamic strings, but it chews them one line at the time.
Ok, care to update this patch with a max size?
And again, does systemd and syslog-ng handle short writes properly?
thanks,
greg k-h
Quoted 18 lines · Greg KH
Quoted 17 lines · Sasha Levin
Quoted 15 lines · Greg KH
Quoted 8 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
I guess a page would be a reasonable size, similar to what we do for sysfs.
Ok. Sasha, as you seem to have noticed this, care to dig in syslog and systemd to get an idea of the buffer sizes they are expecting to pass into kmsg, and if they can handle a short write properly? If so, restricting it to a page is fine with me, otherwise we might want to make it a bit bigger.
Quoted 6 lines
Quoted 2 lines
systemd seems to use posix
LINE_MAXsized buffers, syslog-ng uses dynamic strings, but it chews them one line at the time.Ok, care to update this patch with a max size?
And again, does systemd and syslog-ng handle short writes properly?
Printk has a static scratch buffer of 1024, we can not really process more than that, so we can limit the /dev/kmsg write() to the same size, I guess.
Thanks,
Kay
Quoted 5 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
Per line dmesg output is limited to 1024 anyway.
Quoted 22 lines · Kay Sievers
Quoted 21 lines · Greg KH
Quoted 19 lines · Sasha Levin
Quoted 7 lines · Greg KH
Quoted 6 lines · Arnd Bergmann
Quoted 4 lines · Thomas Gleixner
I think so. This is an interface to inject stuff into dmesg. Limiting that to a reasonable size makes sense. We can probably limit it to something small like 1024, but I don't know about the "ideas" of those folks who think that it's a great idea to do it at all.
Quoted 8 lines
Quoted 2 lines
I guess a page would be a reasonable size, similar to what we do for sysfs.
Ok. Sasha, as you seem to have noticed this, care to dig in syslog and systemd to get an idea of the buffer sizes they are expecting to pass into kmsg, and if they can handle a short write properly? If so, restricting it to a page is fine with me, otherwise we might want to make it a bit bigger.
systemd seems to use posix
LINE_MAXsized buffers, syslog-ng uses dynamic strings, but it chews them one line at the time.
Quoted 7 lines
Quoted 3 lines
Ok, care to update this patch with a max size?
And again, does systemd and syslog-ng handle short writes properly?
Printk has a static scratch buffer of 1024, we can not really process more than that, so we can limit the
/dev/kmsgwrite()to the same size, I guess.
That's odd. I've tested it by writing 8000 chars into /dev/kmsg, and all of them came out on the printk, and I saw all of them in my dmesg.
This means that while printk may be somehow limited to 1024, it's still possible to dump more than that into dmesg.
Quoted 1 line · Greg KH
Ok, care to update this patch with a max size?
Are there any objections to using PAGE_SIZE? If not, I'll send a revised patch.