Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1412908pxf; Fri, 26 Mar 2021 07:24:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwELVuoU5192I4a+TpFypFHl4VQZEjn4dMLflTiatjCTI7UZfrK6dFepJiQ8z5yreWrT3GK X-Received: by 2002:a17:906:5689:: with SMTP id am9mr15099474ejc.298.1616768673413; Fri, 26 Mar 2021 07:24:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616768673; cv=none; d=google.com; s=arc-20160816; b=jX53fMjtRMyKnXnvB6nibIbK0t8DdtdVBvRsBxuiJPXkaz6Mg1V/x2V7oE6hXk9LdW jQ/2F/s5t1sCOmsSkOPjuPZlYIQ8L99PBNYn1ziBHyqF+cGg9Is0tOUYzwlw+QBH4ts3 /vANxutrLAliXkQqAcf87m2xNJiy4fyxKkQ1OBq2x/szBPfPzGrehWw6dB1VCMVmJ4oN VSzHbZf/fAhdmGB8drmSibfZmVAHsXU3GqBovhB3JCAocHBa4K7zqb1GSl2/zRJ9s+mX snBKqYssgtHhciJipNGjkAomAViTe2IkTdVyN+wNrACXBkHYMqLS6kkudN1c6jWFqpBh Wo6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vVTm5TJ1nk7HkS3puWyXBBm2NlZ0XsJiAAIDaevzvYk=; b=fszq0sVqQp1YoqNTpR5d6IKpkuoAXy6ZixV+Lw2dcS8zdeqlv0E0xmzy7PJKEE0jqO uOUxV4F1Qi5uWv1pP89OdsYPFhFaARGlW1V3cHM7ia9ikNqfUdxeVqtfP9nKqqQnC+Ep dL49GyrF17d6HuVupLQZAER/YLde5JqMSqaRdVFsP8Kz3Fi85KFhiq9C7eHbmo/pWB+M IsvZnWLxvqfQpqadBcclPl4zJwNScm1cJlq6mSPtmMbls/9AmVUXLpFfen80ASSX3Uv7 4nFWN8bpgB4LfWGTmcsWexDrmRWXb52rLVxNMvlwAPI9J1FvsY81th7slT3IgFzI/DYE MJDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=W6ZLH0U1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u14si6785976edx.551.2021.03.26.07.24.09; Fri, 26 Mar 2021 07:24:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=W6ZLH0U1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230180AbhCZOW7 (ORCPT + 99 others); Fri, 26 Mar 2021 10:22:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230179AbhCZOWv (ORCPT ); Fri, 26 Mar 2021 10:22:51 -0400 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CABAC0613AA for ; Fri, 26 Mar 2021 07:22:51 -0700 (PDT) Received: by mail-qk1-x732.google.com with SMTP id c3so5338623qkc.5 for ; Fri, 26 Mar 2021 07:22:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vVTm5TJ1nk7HkS3puWyXBBm2NlZ0XsJiAAIDaevzvYk=; b=W6ZLH0U18oGO+yKD+Bvtjh12EmNxzUdJh7nrcT8rgJ2vKW6cX1Y97H0GVOJ6VPmzIT 9OdZg29K/6XoFdAPIcDFx8AZ26GNiSJ9cR/p+EwwHjUQ1UZPmDuD7F1Sly03N3XE8+Td YIInw8q+XvH7RcvsOpbGBoszIfIJeGcw/uTeCGK5JOhDT8ZNC4hTmIlviCZqkRvjee5Y 28DMvf7C/kPKALfSg2rlXlixnP6NyqGO/OIhjUXzYnFOWMbbKxpI9gSu1ZmH+HKbpI9Q /AGcNRbRPpzvDJkP9fbHgVXOc+PQu/K5c4GPH1Tn073UOBmDvEHkW9WsCnORFdSR/Ixi 6d6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vVTm5TJ1nk7HkS3puWyXBBm2NlZ0XsJiAAIDaevzvYk=; b=Y6RWYQ1gGgQxjxtkhpb9vA/61p3MABsUC+SSCKp8ljWUDrAJd1NKAPYK4F+yK2Nz2p 4MzUeBH2AVSzpnHQNUHZtVT3Q06v6EatRJ9jr5RHo7LGJsSSZf6HfrocdFhvwk4QGeYw 6VZRFEoXvCx3/d4eAU3Y/9moHtw9hp6r+L+1k7117P0y4ZCkWEqcot+bZX0S0r+w5/qJ vHFgew0c1bjWaxOarcsy8JDoCUbrmp5CMX074ZKKi8nKVmSxCP1+BArGY9jVs9XoRBuP mPkCTaPnyQkLOFeqXTvIYO4wyqBT2VZTQFxqaNjxFbn7EUtvMIC66thsSEgy+7ih3Mj9 koGg== X-Gm-Message-State: AOAM532W48iRnTjIyHkrJNTCEPSVj6rYcthGeSb/ewDt91PhjngZORqC vXlcWNnZIcscPyoNsG6vdW9ApZOJOtmwTgy35S2qQA== X-Received: by 2002:a37:6658:: with SMTP id a85mr2020532qkc.424.1616768570474; Fri, 26 Mar 2021 07:22:50 -0700 (PDT) MIME-Version: 1.0 References: <00000000000069c40405be6bdad4@google.com> <20210326091207.5si6knxs7tn6rmod@wittgenstein> <20210326135011.wscs4pxal7vvsmmw@wittgenstein> In-Reply-To: <20210326135011.wscs4pxal7vvsmmw@wittgenstein> From: Dmitry Vyukov Date: Fri, 26 Mar 2021 15:22:39 +0100 Message-ID: Subject: Re: [syzbot] KASAN: null-ptr-deref Read in filp_close (2) To: Christian Brauner Cc: syzbot , linux-fsdevel , LKML , syzkaller-bugs , Al Viro Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 26, 2021 at 2:50 PM Christian Brauner wrote: > > On Fri, Mar 26, 2021 at 10:34:28AM +0100, Christian Brauner wrote: > > On Fri, Mar 26, 2021, 10:21 Dmitry Vyukov wrote: > > > > > On Fri, Mar 26, 2021 at 10:12 AM Christian Brauner > > > wrote: > > > > > > > > On Fri, Mar 26, 2021 at 09:02:08AM +0100, Dmitry Vyukov wrote: > > > > > On Fri, Mar 26, 2021 at 8:55 AM syzbot > > > > > wrote: > > > > > > > > > > > > Hello, > > > > > > > > > > > > syzbot found the following issue on: > > > > > > > > > > > > HEAD commit: 5ee96fa9 Merge tag 'irq-urgent-2021-03-21' of git:// > > > git.ke.. > > > > > > git tree: upstream > > > > > > console output: > > > https://syzkaller.appspot.com/x/log.txt?x=17fb84bed00000 > > > > > > kernel config: > > > https://syzkaller.appspot.com/x/.config?x=6abda3336c698a07 > > > > > > dashboard link: > > > https://syzkaller.appspot.com/bug?extid=283ce5a46486d6acdbaf > > > > > > > > > > > > Unfortunately, I don't have any reproducer for this issue yet. > > > > > > > > > > > > IMPORTANT: if you fix the issue, please add the following tag to the > > > commit: > > > > > > Reported-by: syzbot+283ce5a46486d6acdbaf@syzkaller.appspotmail.com > > > > > > > > > > I was able to reproduce this with the following C program: > > > > > > > > https://gist.githubusercontent.com/dvyukov/00fb7aae489f22c60b4e64b45ef14d60/raw/cb368ca523d01986c2917f4414add0893b8f4243/gistfile1.txt > > > > > > > > > > +Christian > > > > > The repro also contains close_range as the previous similar crash: > > > > > > > > https://syzkaller.appspot.com/bug?id=1bef50bdd9622a1969608d1090b2b4a588d0c6ac > > > > > I don't know if it's related or not in this case, but looks suspicious. > > > > > > > > Hm, I fail to reproduce this with your repro. Do you need to have it run > > > > for a long time? > > > > One thing that strucky my eye is that binfmt_misc gets setup which made > > > > me go huh and I see commit > > > > > > > > commit e7850f4d844e0acfac7e570af611d89deade3146 > > > > Author: Lior Ribak > > > > Date: Fri Mar 12 21:07:41 2021 -0800 > > > > > > > > binfmt_misc: fix possible deadlock in bm_register_write > > > > > > > > which uses filp_close() after having called open_exec() on the > > > > interpreter which makes me wonder why this doesn't have to use fput() > > > > like in all other codepaths for binfmnt_*. > > > > > > > > Can you revert this commit and see if you can reproduce this issue. > > > > Maybe this is a complete red herring but worth a try. > > > > > > > > > This program reproduces the crash for me almost immediately. Are you > > > sure you used the right commit/config? > > > > > > > I was trying to reproduce on v5.12-rc3 with all KASAN, KCSAN, KFENCE etc. > > turned on. > > I have an appointment I need to go to but will try to reproduce with commit > > and config you provided when I get home. > > I really hope it's not reproducible with v5.12-rc3 and only later commits > > since that would allow easier bisection. > > Ok, I think I know what's going on. This fixes it for me. Can you test > too, please? I tried the #syz test way but syzbot doesn't have the > reproducer you gave me: The crash does not happen with this patch. Tested-by: Dmitry Vyukov Thanks for the quick fix! > Thank you! > Christian > > From eeb120d02f40b15a925f54ebcf2b4c747c741ad0 Mon Sep 17 00:00:00 2001 > From: Christian Brauner > Date: Fri, 26 Mar 2021 13:33:03 +0100 > Subject: [PATCH] file: fix close_range() for unshare+cloexec > > syzbot reported a bug when putting the last reference to a tasks file > descriptor table. Debugging this showed we didn't recalculate the > current maximum fd number for CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC > after we unshared the file descriptors table. So max_fd could exceed the > current fdtable maximum causing us to set excessive bits. As a concrete > example, let's say the user requested everything from fd 4 to ~0UL to be > closed and their current fdtable size is 256 with their highest open fd > being 4. With CLOSE_RANGE_UNSHARE the caller will end up with a new > fdtable which has room for 64 file descriptors since that is the lowest > fdtable size we accept. But now max_fd will still point to 255 and needs > to be adjusted. Fix this by retrieving the correct maximum fd value in > __range_cloexec(). > > Reported-by: syzbot+283ce5a46486d6acdbaf@syzkaller.appspotmail.com > Cc: Christoph Hellwig > Cc: Al Viro > Cc: linux-fsdevel@vger.kernel.org > Cc: stable@vger.kernel.org > Signed-off-by: Christian Brauner > --- > fs/file.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/fs/file.c b/fs/file.c > index f3a4bac2cbe9..5ef62377d924 100644 > --- a/fs/file.c > +++ b/fs/file.c > @@ -632,6 +632,7 @@ EXPORT_SYMBOL(close_fd); /* for ksys_close() */ > static inline void __range_cloexec(struct files_struct *cur_fds, > unsigned int fd, unsigned int max_fd) > { > + unsigned int cur_max; > struct fdtable *fdt; > > if (fd > max_fd) > @@ -639,7 +640,12 @@ static inline void __range_cloexec(struct files_struct *cur_fds, > > spin_lock(&cur_fds->file_lock); > fdt = files_fdtable(cur_fds); > - bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1); > + /* make very sure we're using the correct maximum value */ > + cur_max = fdt->max_fds; > + cur_max--; > + cur_max = min(max_fd, cur_max); > + if (fd <= cur_max) > + bitmap_set(fdt->close_on_exec, fd, cur_max - fd + 1); > spin_unlock(&cur_fds->file_lock); > } > > -- > 2.27.0 >