linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] proc: Avoid mixing integer types in mem_rw()
@ 2021-05-12 12:52 Marcelo Henrique Cerri
  2021-05-12 13:19 ` Alexey Dobriyan
  0 siblings, 1 reply; 3+ messages in thread
From: Marcelo Henrique Cerri @ 2021-05-12 12:52 UTC (permalink / raw)
  To: linux-fsdevel
  Cc: Marcelo Henrique Cerri, David Disseldorp,
	Thadeu Lima de Souza Cascardo, Alexey Dobriyan, Andrew Morton,
	Christian Brauner, Michel Lespinasse, Helge Deller,
	Oleg Nesterov, Lorenzo Stoakes, linux-kernel

Use size_t when capping the count argument received by mem_rw(). Since
count is size_t, using min_t(int, ...) can lead to a negative value
that will later be passed to access_remote_vm(), which can cause
unexpected behavior.

Since we are capping the value to at maximum PAGE_SIZE, the conversion
from size_t to int when passing it to access_remote_vm() as "len"
shouldn't be a problem.

Reviewed-by: David Disseldorp <ddiss@suse.de>
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
Signed-off-by: Marcelo Henrique Cerri <marcelo.cerri@canonical.com>
---
 fs/proc/base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 3851bfcdba56..8dbc6a1aaadb 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -854,7 +854,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
 	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
 
 	while (count > 0) {
-		int this_len = min_t(int, count, PAGE_SIZE);
+		size_t this_len = min_t(size_t, count, PAGE_SIZE);
 
 		if (write && copy_from_user(page, buf, this_len)) {
 			copied = -EFAULT;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] proc: Avoid mixing integer types in mem_rw()
  2021-05-12 12:52 [PATCH] proc: Avoid mixing integer types in mem_rw() Marcelo Henrique Cerri
@ 2021-05-12 13:19 ` Alexey Dobriyan
  2021-05-12 13:40   ` Marcelo Henrique Cerri
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2021-05-12 13:19 UTC (permalink / raw)
  To: Marcelo Henrique Cerri
  Cc: linux-fsdevel, David Disseldorp, Thadeu Lima de Souza Cascardo,
	Andrew Morton, Christian Brauner, Michel Lespinasse,
	Helge Deller, Oleg Nesterov, Lorenzo Stoakes, linux-kernel

On Wed, May 12, 2021 at 09:52:12AM -0300, Marcelo Henrique Cerri wrote:
> Use size_t when capping the count argument received by mem_rw(). Since
> count is size_t, using min_t(int, ...) can lead to a negative value
> that will later be passed to access_remote_vm(), which can cause
> unexpected behavior.
> 
> Since we are capping the value to at maximum PAGE_SIZE, the conversion
> from size_t to int when passing it to access_remote_vm() as "len"
> shouldn't be a problem.

> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -854,7 +854,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
>  	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
>  
>  	while (count > 0) {
> -		int this_len = min_t(int, count, PAGE_SIZE);
> +		size_t this_len = min_t(size_t, count, PAGE_SIZE);

As much as I don't like signed integers, VFS caps read/write lengths
at INT_MAX & PAGE_MASK, so casting doesn't change values.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] proc: Avoid mixing integer types in mem_rw()
  2021-05-12 13:19 ` Alexey Dobriyan
@ 2021-05-12 13:40   ` Marcelo Henrique Cerri
  0 siblings, 0 replies; 3+ messages in thread
From: Marcelo Henrique Cerri @ 2021-05-12 13:40 UTC (permalink / raw)
  To: Alexey Dobriyan
  Cc: linux-fsdevel, David Disseldorp, Thadeu Lima de Souza Cascardo,
	Andrew Morton, Christian Brauner, Michel Lespinasse,
	Helge Deller, Oleg Nesterov, Lorenzo Stoakes, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1312 bytes --]

On Wed, May 12, 2021 at 04:19:26PM +0300, Alexey Dobriyan wrote:
> On Wed, May 12, 2021 at 09:52:12AM -0300, Marcelo Henrique Cerri wrote:
> > Use size_t when capping the count argument received by mem_rw(). Since
> > count is size_t, using min_t(int, ...) can lead to a negative value
> > that will later be passed to access_remote_vm(), which can cause
> > unexpected behavior.
> > 
> > Since we are capping the value to at maximum PAGE_SIZE, the conversion
> > from size_t to int when passing it to access_remote_vm() as "len"
> > shouldn't be a problem.
> 
> > --- a/fs/proc/base.c
> > +++ b/fs/proc/base.c
> > @@ -854,7 +854,7 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
> >  	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
> >  
> >  	while (count > 0) {
> > -		int this_len = min_t(int, count, PAGE_SIZE);
> > +		size_t this_len = min_t(size_t, count, PAGE_SIZE);
> 
> As much as I don't like signed integers, VFS caps read/write lengths
> at INT_MAX & PAGE_MASK, so casting doesn't change values.

Although that should always be true we had a recent example of a
caller that wasn't properly capping it and since count is unsigned
anyway it makes sense to keep this value positive which might mitigate
similar issues in the future.

-- 
Regards,
Marcelo


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-12 13:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-12 12:52 [PATCH] proc: Avoid mixing integer types in mem_rw() Marcelo Henrique Cerri
2021-05-12 13:19 ` Alexey Dobriyan
2021-05-12 13:40   ` Marcelo Henrique Cerri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).