Skip to content

Commit a65cab7

Browse files
ebiggersgregkh
authored andcommitted
libfs: fix infoleak in simple_attr_read()
Reading from a debugfs file at a nonzero position, without first reading at position 0, leaks uninitialized memory to userspace. It's a bit tricky to do this, since lseek() and pread() aren't allowed on these files, and write() doesn't update the position on them. But writing to them with splice() *does* update the position: #define _GNU_SOURCE 1 #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main() { int pipes[2], fd, n, i; char buf[32]; pipe(pipes); write(pipes[1], "0", 1); fd = open("/sys/kernel/debug/fault_around_bytes", O_RDWR); splice(pipes[0], NULL, fd, NULL, 1, 0); n = read(fd, buf, sizeof(buf)); for (i = 0; i < n; i++) printf("%02x", buf[i]); printf("\n"); } Output: 5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a30 Fix the infoleak by making simple_attr_read() always fill simple_attr::get_buf if it hasn't been filled yet. Reported-by: [email protected] Reported-by: Alexander Potapenko <[email protected]> Fixes: acaefc2 ("[PATCH] libfs: add simple attribute files") Cc: [email protected] Signed-off-by: Eric Biggers <[email protected]> Acked-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4dbe191 commit a65cab7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

fs/libfs.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ int simple_attr_open(struct inode *inode, struct file *file,
891891
{
892892
struct simple_attr *attr;
893893

894-
attr = kmalloc(sizeof(*attr), GFP_KERNEL);
894+
attr = kzalloc(sizeof(*attr), GFP_KERNEL);
895895
if (!attr)
896896
return -ENOMEM;
897897

@@ -931,9 +931,11 @@ ssize_t simple_attr_read(struct file *file, char __user *buf,
931931
if (ret)
932932
return ret;
933933

934-
if (*ppos) { /* continued read */
934+
if (*ppos && attr->get_buf[0]) {
935+
/* continued read */
935936
size = strlen(attr->get_buf);
936-
} else { /* first read */
937+
} else {
938+
/* first read */
937939
u64 val;
938940
ret = attr->get(attr->data, &val);
939941
if (ret)

0 commit comments

Comments
 (0)