Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp5326443pxv; Wed, 7 Jul 2021 00:46:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgrA3uJ9dRQtEWwBHr4jl6V76U5xrCXtTlyRCPJW+Xwdt2TEmCy2V1LmIdaTpFQWOX/vna X-Received: by 2002:aa7:c450:: with SMTP id n16mr27886004edr.58.1625644005086; Wed, 07 Jul 2021 00:46:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625644005; cv=none; d=google.com; s=arc-20160816; b=gtNKOrYWLmO1nzTrbRa9qNuN6mCRx07x6iDM1W+ddTutAy5hy4cM2h1ZTpxkWnyYK0 ddutOBZQ8HHIyfdyYco014/XbPc/uuE1zVxEw6sfqAWqlj2eTFkDQJ7xdxmmFNYw8oHB nwTQ93joKyRtWlRovTuZVKsuvI3zSitfzP2q/zTO2mb+qLhhmIijUmbcJS4TfGQNszVP XGgV1RtCCsMZLiTqmADzlG/Zy6m+3N6AXec3oCAXuVe3+SsaTpEj7+zzhBMqNE5DgXNJ U7wzYjIlIICWwt9BeTTIS2PSzSSRvKjonERhWxyYNgCVvT7SbatmW2i7sm3P4fQAHy23 eB8w== 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=mnaKooiXJs6zXYO5+MRv0r+1pvlB1aCOce0LmiWEYEQ=; b=masJCQJSLtTJ4CjjaZkr+ZZPYR3yqa5lCM2RUJNucgUyIEDJ5zF0jvD+EJu/a2tlCs j51k9IIMNleQ2bfdPZaPbmLEAy2EI8K+bYqW+YMbLPNGpiOYbNPH/YHEA9Ix/GsQm+kd hlkyTCxUvtOxt+MNiFpTXyoHfxFNin0AAKwLmYImr/HprbIqZSN71/3DOmJQlPPjg4HC eXYX1GIKWs6MVMIV1tI9XzLRNUkMFNmiCxIKfyZiPx7jbzP977J+w6EmTSvY8ZAfl2RZ wyIh+o5R3DpGNR1N3lHwNIC89r0Ydrhz5OaMWtVktsf0PSMpBkHbpLxMJQW2cQm0lhiW 9USg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=gV9bJFmo; 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 s11si17217805edd.219.2021.07.07.00.46.21; Wed, 07 Jul 2021 00:46:45 -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=gV9bJFmo; 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 S230511AbhGGHrZ (ORCPT + 99 others); Wed, 7 Jul 2021 03:47:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230356AbhGGHrY (ORCPT ); Wed, 7 Jul 2021 03:47:24 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4889BC061574; Wed, 7 Jul 2021 00:44:44 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id h1-20020a17090a3d01b0290172d33bb8bcso3197840pjc.0; Wed, 07 Jul 2021 00:44:44 -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=mnaKooiXJs6zXYO5+MRv0r+1pvlB1aCOce0LmiWEYEQ=; b=gV9bJFmoma8pntKDpIj40dn6InHYTtTt98wAIjVmMtj+3PAZQVAghhOixxM87Xai/e xopDw7TLWc3rVWyJWXvWP652wWT2Z/y7yf3OLHtBGK+FRg/kih02RPBj3IdVnlMVhAPj yUu7ebtwsMQfAtATIYFIlUrzmgJOOouydyF7VGzLImEENlUbRsa1zuw5RdJflesOvHjh PlSdsJTvLDMf4yP3OyaI+oLbnhQBBsd1+W+ZqdFqhHz18MD7VC1SOdSynXMI+Hukpre4 IyGTi50k/co14g7Sk620psXXd3gzNv6Gz03C4fXm3qLzishZxMYbpn1pREvq0WLtwuQ8 1D2g== 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=mnaKooiXJs6zXYO5+MRv0r+1pvlB1aCOce0LmiWEYEQ=; b=KwhY1jHOXl8asBrl+eU5d0RYBzpbHCgd+ZhZTaIWnxx0mtrf7Fy1U3gTH7pITxjbof ElKLBRSftsYM0/lYPRKIWQbZqjbjJMQ8zLi6OmvLnuf/+WB9xY1ONaZWADLtVyBYGwM3 GLjHoWm0xRyl9C0CHxHk49cc6D9yOxHe4Ubc4TDWzh0JU+K9nMmGBsjC7xJU7pPX8Pz+ Uu4+Oh5zfzMdkjcmIfMsZCx8l+6H7+gLkA9/lCSirPlcRbFxFsGrmgeE0HAXUjd5GwBe jZIxiRbSWkbiVCgNWSQmFnSxP3IuYb2l6vqX1CzRYm60U3i4lFla2Yfp3iEQ6oB3gbMz +JOA== X-Gm-Message-State: AOAM530UMcU3XP2neVQ4Xl0Q4FdmNkc3lfCtxpqJiNuUnpae+TMmRYlS L+u9dgLA+AO0HI4McD5auEc= X-Received: by 2002:a17:902:8c83:b029:129:17e5:a1cc with SMTP id t3-20020a1709028c83b029012917e5a1ccmr20378088plo.49.1625643883878; Wed, 07 Jul 2021 00:44:43 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id x39sm12958519pfu.81.2021.07.07.00.44.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 00:44:43 -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 v3 1/2] fcntl: fix potential deadlocks for &fown_struct.lock Date: Wed, 7 Jul 2021 15:44:00 +0800 Message-Id: <20210707074401.447952-2-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707074401.447952-1-desmondcheongzx@gmail.com> References: <20210707074401.447952-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 are 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() Interrupts are not disabled on either path. Reported-and-tested-by: syzbot+e6d5398a02c516ce5e70@syzkaller.appspotmail.com Signed-off-by: Desmond Cheong Zhi Xi --- fs/fcntl.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index dfc72f15be7f..cf9e81dfa615 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -150,7 +150,8 @@ void f_delown(struct file *filp) pid_t f_getown(struct file *filp) { pid_t pid = 0; - read_lock(&filp->f_owner.lock); + + 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 +159,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 +209,7 @@ 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); + 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 +232,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 +250,10 @@ static int f_getowner_uids(struct file *filp, unsigned long arg) uid_t src[2]; int err; - read_lock(&filp->f_owner.lock); + 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