Linux-Modules Archive mirror
 help / color / mirror / Atom feed
From: Vegard Nossum <vegard.nossum@oracle.com>
To: Luis Chamberlain <mcgrof@kernel.org>
Cc: George Kennedy <george.kennedy@oracle.com>,
	linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org,
	Vegard Nossum <vegard.nossum@oracle.com>,
	Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>,
	syzbot+9c2bdc9d24e4a7abe741@syzkaller.appspotmail.com,
	Johan Hovold <johan@kernel.org>,
	Dan Williams <dan.j.williams@intel.com>,
	Rudi Heitbaum <rudi@heitbaum.com>,
	David Hildenbrand <david@redhat.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH] module: always complete idempotent loads
Date: Tue,  4 Jul 2023 12:08:52 +0200	[thread overview]
Message-ID: <20230704100852.23452-1-vegard.nossum@oracle.com> (raw)
In-Reply-To: <20230704092309.22669-1-vegard.nossum@oracle.com>

Commit 9b9879fc0327 added a hashtable storing lists of concurrent module
loads. However, it didn't fix up all the error paths in
init_module_from_file(); this would lead to leaving the function while an
on-stack 'struct idempotent' element is still in the hash table, which
leads to all sorts of badness as spotted by syzkaller:

    BUG: soft lockup in sys_finit_module
    BUG: unable to handle kernel paging request in init_module_from_file
    general protection fault in init_module_from_file
    INFO: task hung in init_module_from_file
    KASAN: out-of-bounds Read in init_module_from_file
    KASAN: slab-out-of-bounds Read in init_module_from_file
    KASAN: slab-use-after-free Read in init_module_from_file
    KASAN: slab-use-after-free Read in set_powered_sync
    KASAN: stack-out-of-bounds Read in init_module_from_file
    KASAN: use-after-free in init_module_from_file
    KASAN: use-after-free Read in init_module_from_file

Fixes: 9b9879fc0327 ("modules: catch concurrent module loads, treat them as idempotent")
Reported-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Reported-by: syzbot+9c2bdc9d24e4a7abe741@syzkaller.appspotmail.com
Tested-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Cc: Johan Hovold <johan@kernel.org>
Cc: Johan Hovold <johan@kernel.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Rudi Heitbaum <rudi@heitbaum.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
---
 kernel/module/main.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index d6de66a6a1ac..6100d0373f2f 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -3121,39 +3121,42 @@ static void idempotent_complete(struct idempotent *u, int ret)
 static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
 {
 	struct idempotent idem;
 	struct load_info info = { };
 	void *buf = NULL;
 	int len, ret;
 
 	if (!f || !(f->f_mode & FMODE_READ))
 		return -EBADF;
 
 	if (idempotent(&idem, file_inode(f))) {
 		wait_for_completion(&idem.complete);
 		return idem.ret;
 	}
 
 	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
 	if (len < 0) {
 		mod_stat_inc(&failed_kreads);
 		mod_stat_add_long(len, &invalid_kread_bytes);
-		return len;
+		ret = len;
+		goto out;
 	}
 
 	if (flags & MODULE_INIT_COMPRESSED_FILE) {
 		int err = module_decompress(&info, buf, len);
 		vfree(buf); /* compressed data is no longer needed */
 		if (err) {
 			mod_stat_inc(&failed_decompress);
 			mod_stat_add_long(len, &invalid_decompress_bytes);
-			return err;
+			ret = err;
+			goto out;
 		}
 	} else {
 		info.hdr = buf;
 		info.len = len;
 	}
 
 	ret = load_module(&info, uargs, flags);
+out:
 	idempotent_complete(&idem, ret);
 	return ret;
 }
-- 


       reply	other threads:[~2023-07-04 10:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230704092309.22669-1-vegard.nossum@oracle.com>
2023-07-04 10:08 ` Vegard Nossum [this message]
2023-07-04 13:37   ` [PATCH] module: always complete idempotent loads Linus Torvalds
2023-07-04 15:11     ` Vegard Nossum
2023-07-04 17:10       ` Linus Torvalds
2023-07-04 18:28     ` Vegard Nossum
2023-07-04 18:38       ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230704100852.23452-1-vegard.nossum@oracle.com \
    --to=vegard.nossum@oracle.com \
    --cc=dan.j.williams@intel.com \
    --cc=david@redhat.com \
    --cc=george.kennedy@oracle.com \
    --cc=harshit.m.mogalapalli@oracle.com \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=rudi@heitbaum.com \
    --cc=syzbot+9c2bdc9d24e4a7abe741@syzkaller.appspotmail.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).