Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/buildid: use __kernel_read() for sleepable context

Prevent a "BUG: unable to handle kernel NULL pointer dereference in
filemap_read_folio".

For the sleepable context, convert freader to use __kernel_read() instead
of direct page cache access via read_cache_folio(). This simplifies the
faultable code path by using the standard kernel file reading interface
which handles all the complexity of reading file data.

At the moment we are not changing the code for non-sleepable context which
uses filemap_get_folio() and only succeeds if the target folios are
already in memory and up-to-date. The reason is to keep the patch simple
and easier to backport to stable kernels.

Syzbot repro does not crash the kernel anymore and the selftests run
successfully.

In the follow up we will make __kernel_read() with IOCB_NOWAIT work for
non-sleepable contexts. In addition, I would like to replace the
secretmem check with a more generic approach and will add fstest for the
buildid code.

Link: https://lkml.kernel.org/r/20251222205859.3968077-1-shakeel.butt@linux.dev
Fixes: ad41251c290d ("lib/buildid: implement sleepable build_id_parse() API")
Reported-by: syzbot+09b7d050e4806540153d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=09b7d050e4806540153d
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Tested-by: Jinchao Wang <wangjinchao600@gmail.com>
Link: https://lkml.kernel.org/r/aUteBPWPYzVWIZFH@ndev
Reviewed-by: Christian Brauner <brauner@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: "Darrick J. Wong" <djwong@kernel.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Shakeel Butt and committed by
Andrew Morton
777a8560 6ac166a0

+20 -12
+20 -12
lib/buildid.c
··· 5 5 #include <linux/elf.h> 6 6 #include <linux/kernel.h> 7 7 #include <linux/pagemap.h> 8 + #include <linux/fs.h> 8 9 #include <linux/secretmem.h> 9 10 10 11 #define BUILD_ID 3 ··· 47 46 48 47 freader_put_folio(r); 49 48 50 - /* reject secretmem folios created with memfd_secret() */ 51 - if (secretmem_mapping(r->file->f_mapping)) 52 - return -EFAULT; 53 - 49 + /* only use page cache lookup - fail if not already cached */ 54 50 r->folio = filemap_get_folio(r->file->f_mapping, file_off >> PAGE_SHIFT); 55 - 56 - /* if sleeping is allowed, wait for the page, if necessary */ 57 - if (r->may_fault && (IS_ERR(r->folio) || !folio_test_uptodate(r->folio))) { 58 - filemap_invalidate_lock_shared(r->file->f_mapping); 59 - r->folio = read_cache_folio(r->file->f_mapping, file_off >> PAGE_SHIFT, 60 - NULL, r->file); 61 - filemap_invalidate_unlock_shared(r->file->f_mapping); 62 - } 63 51 64 52 if (IS_ERR(r->folio) || !folio_test_uptodate(r->folio)) { 65 53 if (!IS_ERR(r->folio)) ··· 85 95 return NULL; 86 96 } 87 97 return r->data + file_off; 98 + } 99 + 100 + /* reject secretmem folios created with memfd_secret() */ 101 + if (secretmem_mapping(r->file->f_mapping)) { 102 + r->err = -EFAULT; 103 + return NULL; 104 + } 105 + 106 + /* use __kernel_read() for sleepable context */ 107 + if (r->may_fault) { 108 + ssize_t ret; 109 + 110 + ret = __kernel_read(r->file, r->buf, sz, &file_off); 111 + if (ret != sz) { 112 + r->err = (ret < 0) ? ret : -EIO; 113 + return NULL; 114 + } 115 + return r->buf; 88 116 } 89 117 90 118 /* fetch or reuse folio for given file offset */