]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
kfifo: fix ternary sign extension bugs
authorDan Carpenter <dan.carpenter@oracle.com>
Fri, 30 Apr 2021 05:54:15 +0000 (22:54 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 22 May 2021 08:38:26 +0000 (10:38 +0200)
[ Upstream commit 926ee00ea24320052b46745ef4b00d91c05bd03d ]

The intent with this code was to return negative error codes but instead
it returns positives.

The problem is how type promotion works with ternary operations.  These
functions return long, "ret" is an int and "copied" is a u32.  The
negative error code is first cast to u32 so it becomes a high positive and
then cast to long where it's still a positive.

We could fix this by declaring "ret" as a ssize_t but let's just get rid
of the ternaries instead.

Link: https://lkml.kernel.org/r/YIE+/cK1tBzSuQPU@mwanda
Fixes: 5bf2b19320ec ("kfifo: add example files to the kernel sample directory")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Stefani Seibold <stefani@seibold.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
samples/kfifo/bytestream-example.c
samples/kfifo/inttype-example.c
samples/kfifo/record-example.c

index 2fca916d9edfd7a5a70a6e7e0a5df9fe2766d3a9..a7f5ee8b6edcf397a2da0d54e497b509d6241444 100644 (file)
@@ -124,8 +124,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
        ret = kfifo_from_user(&test, buf, count, &copied);
 
        mutex_unlock(&write_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static ssize_t fifo_read(struct file *file, char __user *buf,
@@ -140,8 +142,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
        ret = kfifo_to_user(&test, buf, count, &copied);
 
        mutex_unlock(&read_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static const struct file_operations fifo_fops = {
index 8dc3c2e7105a0474b5638c208a4acfc63c36a93e..a326a37e916312d40d15455f26ca6acccf82b519 100644 (file)
@@ -117,8 +117,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
        ret = kfifo_from_user(&test, buf, count, &copied);
 
        mutex_unlock(&write_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static ssize_t fifo_read(struct file *file, char __user *buf,
@@ -133,8 +135,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
        ret = kfifo_to_user(&test, buf, count, &copied);
 
        mutex_unlock(&read_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static const struct file_operations fifo_fops = {
index 2d7529eeb2940a8d6f92cc2104e0f8379dcb74ca..deb87a2e4e6bc5109f682b527908556bce635ee0 100644 (file)
@@ -131,8 +131,10 @@ static ssize_t fifo_write(struct file *file, const char __user *buf,
        ret = kfifo_from_user(&test, buf, count, &copied);
 
        mutex_unlock(&write_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static ssize_t fifo_read(struct file *file, char __user *buf,
@@ -147,8 +149,10 @@ static ssize_t fifo_read(struct file *file, char __user *buf,
        ret = kfifo_to_user(&test, buf, count, &copied);
 
        mutex_unlock(&read_lock);
+       if (ret)
+               return ret;
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static const struct file_operations fifo_fops = {