Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp5164447pxv; Tue, 6 Jul 2021 19:38:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwm2yA7umMRKcZYsWTG3bJfnx7m1KfN+9I9gcJxVTVW3S3hCPUpTYe9/KABugYdMNH9buP X-Received: by 2002:a17:907:968e:: with SMTP id hd14mr10863689ejc.328.1625625521983; Tue, 06 Jul 2021 19:38:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625625521; cv=none; d=google.com; s=arc-20160816; b=iaUhgDGmNaop1INB+NZiuw/afu1PSyeRO55v4bk291qt9WF1u26E/kKsvyVNMVGwhY viOhRqr+iQgsyFKnAv8zRb69UFEODwUr/1qSnz9ncIx7PbLVWzaHwfwT6Zh1kSRouW1g 3mqzhNuUmEM5+EgGYJFEPWn3J4UuourMYIckjhsI0XOM9v5I9HjNWVxgIO1udcXfbruN HtrfgMAxkLFap7wLf2BTW+81IHFSUleif4ky3DuU1Q4FvijPupZjHHF+EIQRnKNQWoW9 X6Kc1GJXKhZCXz2/H8TtGLHsioQivKeDvYNCJ4QwCFdafoS/z5mcewy8hmqftIevGz/q B8rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=56B++SYcNkGDLySJ62AZWIU2HnskBoJwEPVYnQNmO+k=; b=Hb6lv1NaZ9ckEvPgi8rXmnUeT/ju9rRUsx5lj1rglbt3i3dDT9C8tp9F2NvaXej2ov dhpWB5jtKlTeTPUPyGUZePmOa064YNAfOGAfOxArFmbW+EFVUwGYLhr/Z+Afcgf4P1O+ BKn81co3oqKOzO9msMrJdXt7Z0aBgKBYRDRl+HOhhpjG7RbaTzZ82UIZkepBbgLYZ40D e9WWkjn7PP4AEKMCMbF//8ABAHyWfFbWFZwLezIb7FUXU/tXCULpa3DWqO++pnXr4OeH 9GTAdAY/srSYCLvVvCncvR1OtfVm/tZZW/Je2zh5nYfhKFUZhFs2TAsAMVYrc9V0sohz Ikbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="GKET/NY1"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t11si1415209ejd.8.2021.07.06.19.37.57; Tue, 06 Jul 2021 19:38:41 -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=@gmail.com header.s=20161025 header.b="GKET/NY1"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230012AbhGGCjM (ORCPT + 99 others); Tue, 6 Jul 2021 22:39:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbhGGCjL (ORCPT ); Tue, 6 Jul 2021 22:39:11 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A81AC061760; Tue, 6 Jul 2021 19:36:31 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id s13so224628plg.12; Tue, 06 Jul 2021 19:36:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=56B++SYcNkGDLySJ62AZWIU2HnskBoJwEPVYnQNmO+k=; b=GKET/NY1Nx2Y5xu+oDqx0/TbbJiMtBTMgoBUSBbsMBHh9ds59E661pkGTnSp7zFS+j km54h2aSpXdZOjEWrlVBj1sC94cDVYW3MfTckbHWXhW1aWUEvSWZ73msF5FPwXv0Y5Gx yPfV8xYFMV5aeQb31xdtXeylWm+7PCDoQS1gh9pgdwpAZch4pJeAapTLu7w/tg2VMO5E OplE+SaYd9ZbzUBqDL/Bdtg0hdADpRJRHN9qYk9P56chRIKr1bVrYpeua826UXZBreBE hG0T5Yg5d8D2MTVSJBRMpjUFU8uru4qhvDa8P4nPzrDesmk5jtP9m5wnFx0MYfZTZ7d2 cdlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=56B++SYcNkGDLySJ62AZWIU2HnskBoJwEPVYnQNmO+k=; b=KfexMjeypb3nfMa+4y9qi8/Z/NSz4tzzbmo/5UzEyEpC0LHkaqCEVLpe70ZoSCW1CG wm6rRd2orS7DhM3KhiQaD3y2cnJ5Wp/RaBmmSbTHcw4i0IjZJHjXW8bg5GMp0w2bETzz czWsnXa4Z9qeGUIaFiS+K4Cuv0bWwaD1D4zXrdNyRM8Fehvm+kfMxBX81G7w4IT8vYaV OyFNrh1+5MOW1BRYkJPcQAZXv3IxLFFMTP7kUw5/nDmD3IkXztKYen8ZXkpiml2tAzmf sZTxtGrcBbCj+RizhvFQXPEOzorClYIV1cuX+CxBdXflsinmNzUhc+DOvv/fwR1zCw1A HbSA== X-Gm-Message-State: AOAM531CQR6PfNql79TgqI6/DtXUOt0yCuY5wMiMEMbblOiZ6nwsPR+D UCttjZmntvrkPp8m5FK+nZc= X-Received: by 2002:a17:902:d213:b029:127:9520:7649 with SMTP id t19-20020a170902d213b029012795207649mr19497260ply.10.1625625390833; Tue, 06 Jul 2021 19:36:30 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id r14sm20589446pgm.28.2021.07.06.19.36.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 19:36:30 -0700 (PDT) From: Desmond Cheong Zhi Xi To: jlayton@kernel.org, bfields@fieldses.org, viro@zeniv.linux.org.uk Cc: Desmond Cheong Zhi Xi , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+e6d5398a02c516ce5e70@syzkaller.appspotmail.com Subject: [PATCH v2 1/2] fcntl: fix potential deadlocks for &fown_struct.lock Date: Wed, 7 Jul 2021 10:35:47 +0800 Message-Id: <20210707023548.15872-2-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707023548.15872-1-desmondcheongzx@gmail.com> References: <20210707023548.15872-1-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Syzbot reports a potential deadlock in do_fcntl: ======================================================== WARNING: possible irq lock inversion dependency detected 5.12.0-syzkaller #0 Not tainted -------------------------------------------------------- syz-executor132/8391 just changed the state of lock: ffff888015967bf8 (&f->f_owner.lock){.+..}-{2:2}, at: f_getown_ex fs/fcntl.c:211 [inline] ffff888015967bf8 (&f->f_owner.lock){.+..}-{2:2}, at: do_fcntl+0x8b4/0x1200 fs/fcntl.c:395 but this lock was taken by another, HARDIRQ-safe lock in the past: (&dev->event_lock){-...}-{2:2} and interrupts could create inverse lock ordering between them. other info that might help us debug this: Chain exists of: &dev->event_lock --> &new->fa_lock --> &f->f_owner.lock Possible interrupt unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&f->f_owner.lock); local_irq_disable(); lock(&dev->event_lock); lock(&new->fa_lock); lock(&dev->event_lock); *** DEADLOCK *** This happens because there is a lock hierarchy of &dev->event_lock --> &new->fa_lock --> &f->f_owner.lock from the following call chain: input_inject_event(): spin_lock_irqsave(&dev->event_lock,...); input_handle_event(): input_pass_values(): input_to_handler(): evdev_events(): evdev_pass_values(): spin_lock(&client->buffer_lock); __pass_event(): kill_fasync(): kill_fasync_rcu(): read_lock(&fa->fa_lock); send_sigio(): read_lock_irqsave(&fown->lock,...); However, since &dev->event_lock is HARDIRQ-safe, interrupts have to be disabled while grabbing &f->f_owner.lock, otherwise we invert the lock hierarchy. Hence, we replace calls to read_lock/read_unlock on &f->f_owner.lock, with read_lock_irq/read_unlock_irq. Here read_lock_irq/read_unlock_irq should be safe to use because the functions f_getown_ex and f_getowner_uids are only called from do_fcntl, and f_getown is only called from do_fnctl and sock_ioctl. do_fnctl itself is only called from syscalls. For sock_ioctl, the chain is compat_sock_ioctl(): compat_sock_ioctl_trans(): sock_ioctl() And interrupts are not disabled on either path. We assert this assumption with WARN_ON_ONCE(irqs_disabled()). This check is also inserted into another use of write_lock_irq in f_modown. Reported-and-tested-by: syzbot+e6d5398a02c516ce5e70@syzkaller.appspotmail.com Signed-off-by: Desmond Cheong Zhi Xi --- fs/fcntl.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index dfc72f15be7f..262235e02c4b 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -88,6 +88,7 @@ static int setfl(int fd, struct file * filp, unsigned long arg) static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, int force) { + WARN_ON_ONCE(irqs_disabled()); write_lock_irq(&filp->f_owner.lock); if (force || !filp->f_owner.pid) { put_pid(filp->f_owner.pid); @@ -150,7 +151,9 @@ void f_delown(struct file *filp) pid_t f_getown(struct file *filp) { pid_t pid = 0; - read_lock(&filp->f_owner.lock); + + WARN_ON_ONCE(irqs_disabled()); + read_lock_irq(&filp->f_owner.lock); rcu_read_lock(); if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) { pid = pid_vnr(filp->f_owner.pid); @@ -158,7 +161,7 @@ pid_t f_getown(struct file *filp) pid = -pid; } rcu_read_unlock(); - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); return pid; } @@ -208,7 +211,8 @@ static int f_getown_ex(struct file *filp, unsigned long arg) struct f_owner_ex owner = {}; int ret = 0; - read_lock(&filp->f_owner.lock); + WARN_ON_ONCE(irqs_disabled()); + read_lock_irq(&filp->f_owner.lock); rcu_read_lock(); if (pid_task(filp->f_owner.pid, filp->f_owner.pid_type)) owner.pid = pid_vnr(filp->f_owner.pid); @@ -231,7 +235,7 @@ static int f_getown_ex(struct file *filp, unsigned long arg) ret = -EINVAL; break; } - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); if (!ret) { ret = copy_to_user(owner_p, &owner, sizeof(owner)); @@ -249,10 +253,11 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) uid_t src[2]; int err; - read_lock(&filp->f_owner.lock); + WARN_ON_ONCE(irqs_disabled()); + read_lock_irq(&filp->f_owner.lock); src[0] = from_kuid(user_ns, filp->f_owner.uid); src[1] = from_kuid(user_ns, filp->f_owner.euid); - read_unlock(&filp->f_owner.lock); + read_unlock_irq(&filp->f_owner.lock); err = put_user(src[0], &dst[0]); err |= put_user(src[1], &dst[1]); -- 2.25.1