From patchwork Thu Jul 5 04:04:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Khalid Elmously X-Patchwork-Id: 939636 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41Lkl702ntz9s3C; Thu, 5 Jul 2018 14:04:34 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1favVM-0002qN-Cj; Thu, 05 Jul 2018 04:04:28 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1favVL-0002q4-4o for kernel-team@lists.ubuntu.com; Thu, 05 Jul 2018 04:04:27 +0000 Received: from mail-it0-f71.google.com ([209.85.214.71]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1favVK-0004GA-Qj for kernel-team@lists.ubuntu.com; Thu, 05 Jul 2018 04:04:26 +0000 Received: by mail-it0-f71.google.com with SMTP id l82-v6so4929254ita.5 for ; Wed, 04 Jul 2018 21:04:26 -0700 (PDT) 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; bh=UEAaZpvyhQiTx3J+tY+IpPLonDOQ6tT6AbxtPla94VI=; b=FN7Ypxt4CIBbGsdI+zwvPrgC+/9fGasRi2lZm0/Y48xEsK4wUA3OeXSK6AjufJMoxw VjmZ1cBooHDogtk67MSp+R1gP0gj76SGi0nwyx9ztc8N1ToGfjDIvYL4L4GVUcaY04Uz a6/PV3nZnWvObmqrh89cAtbGuUx79YlHo7X+lhpKzzI41IZm1Zk0K2DmODrBsbPCNGmm pPIPSZTdI51QJB4c6WBCVpMU/49UN1Ab04S1d0+HlxfnaOBA0w4dcd8RQeAfEUnaVgJE 2cIG70dU5krwQz03oVFb5NKZSMOh1s+NYxvR0UMjxX3LtP4WrcL4X5nxJygU7SqoHO5C sANg== X-Gm-Message-State: APt69E1rRFs+/O2PH48i1rVnIXFNLczHyxCuNeIsUgbshtERKpxJAion l9JQnjnoUGIjkENKNB964WGloba1jdZnVW8F8TIIyJsX527koKpX12drpv/TuT1POvmyYS6uuji 5lhyk0zieoGbhZjeTRXo+8yPnu1y09RKbaI7Jwz07hg== X-Received: by 2002:a6b:e913:: with SMTP id u19-v6mr3672516iof.38.1530763465545; Wed, 04 Jul 2018 21:04:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfnNrFU8JRdHZae0PBs4XQOK+kLjFtN0geG6x6teoDEvN+QW0J1WYXSFTuxHvvKCjNE8kCnRQ== X-Received: by 2002:a6b:e913:: with SMTP id u19-v6mr3672505iof.38.1530763465386; Wed, 04 Jul 2018 21:04:25 -0700 (PDT) Received: from kbuntu.fuzzbuzz.org (198-84-180-15.cpe.teksavvy.com. [198.84.180.15]) by smtp.gmail.com with ESMTPSA id j11-v6sm2564871itb.16.2018.07.04.21.04.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 21:04:24 -0700 (PDT) From: Khalid Elmously To: kernel-team@lists.ubuntu.com Subject: [SRU][A/B]PATCH 1/1] socket: close race condition between sock_close() and sockfs_setattr() Date: Thu, 5 Jul 2018 00:04:15 -0400 Message-Id: <20180705040415.7962-2-khalid.elmously@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180705040415.7962-1-khalid.elmously@canonical.com> References: <20180705040415.7962-1-khalid.elmously@canonical.com> X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Cong Wang CVE-2018-12232 fchownat() doesn't even hold refcnt of fd until it figures out fd is really needed (otherwise is ignored) and releases it after it resolves the path. This means sock_close() could race with sockfs_setattr(), which leads to a NULL pointer dereference since typically we set sock->sk to NULL in ->release(). As pointed out by Al, this is unique to sockfs. So we can fix this in socket layer by acquiring inode_lock in sock_close() and checking against NULL in sockfs_setattr(). sock_release() is called in many places, only the sock_close() path matters here. And fortunately, this should not affect normal sock_close() as it is only called when the last fd refcnt is gone. It only affects sock_close() with a parallel sockfs_setattr() in progress, which is not common. Fixes: 86741ec25462 ("net: core: Add a UID field to struct sock.") Reported-by: shankarapailoor Cc: Tetsuo Handa Cc: Lorenzo Colitti Cc: Al Viro Signed-off-by: Cong Wang Signed-off-by: David S. Miller (cherry-picked from 6d8c50dcb029872b298eea68cc6209c866fd3e14) Signed-off-by: Khalid Elmously Acked-by: Stefan Bader Acked-by: Kleber Sacilotto de Souza --- net/socket.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/net/socket.c b/net/socket.c index 6f05d5c4bf30..f2957aa205e6 100644 --- a/net/socket.c +++ b/net/socket.c @@ -544,7 +544,10 @@ static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr) if (!err && (iattr->ia_valid & ATTR_UID)) { struct socket *sock = SOCKET_I(d_inode(dentry)); - sock->sk->sk_uid = iattr->ia_uid; + if (sock->sk) + sock->sk->sk_uid = iattr->ia_uid; + else + err = -ENOENT; } return err; @@ -594,12 +597,16 @@ EXPORT_SYMBOL(sock_alloc); * an inode not a file. */ -void sock_release(struct socket *sock) +static void __sock_release(struct socket *sock, struct inode *inode) { if (sock->ops) { struct module *owner = sock->ops->owner; + if (inode) + inode_lock(inode); sock->ops->release(sock); + if (inode) + inode_unlock(inode); sock->ops = NULL; module_put(owner); } @@ -614,6 +621,11 @@ void sock_release(struct socket *sock) } sock->file = NULL; } + +void sock_release(struct socket *sock) +{ + __sock_release(sock, NULL); +} EXPORT_SYMBOL(sock_release); void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags) @@ -1128,7 +1140,7 @@ static int sock_mmap(struct file *file, struct vm_area_struct *vma) static int sock_close(struct inode *inode, struct file *filp) { - sock_release(SOCKET_I(inode)); + __sock_release(SOCKET_I(inode), inode); return 0; }