Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp253309imm; Wed, 4 Jul 2018 23:02:29 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc+R2hhcOwkPUON1UvBgfk8c8p0DR8Q4depZ2/+k1eNTdAq92ltgi53XN+mWS/Npc9zG80C X-Received: by 2002:a17:902:24c7:: with SMTP id l7-v6mr4785731plg.170.1530770549189; Wed, 04 Jul 2018 23:02:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530770549; cv=none; d=google.com; s=arc-20160816; b=z8HMR8CjTWH/FIMthFAJEgE1DefAUlru/v/8PMraORF5wNebOhhd5OT/HCQW/pMBwP xdOfrrAkdBuPx2ROoHsd0tTxHK74EQw9OyWgMomE7gICxQtwBUdOuvXM89WJrNbpsuCa fUxQ75Th3D90XPJGQw2W1GgWhZKENlRa7vxdWoIu3ho9wRZzSbaHg33LECDGnIEIegZL yLC+/JZDq2sqrnJRrO4ZBbFnhylVGumFwPxu+5XUeoJKD3Xe2PldMNAfhEKaO5kmAPPt wXm41CM89qC0q5SYuvEKIuX+b9ilWJisKeeHrs8GuQkObYcBOgy2Uo5Lwq3sLOA4tBH0 EKYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=5MVFn38qCec6lrkiQKmwFoZWAowYV/VmZ/hr9waL54M=; b=lgoHJOsdTtbg+/ZWeZ5Vvobqt/1QpfkXWO3cokX0tacqdSKrn5I1mEeQDrdj75YtTn QhH+v+kY4hl48NEUqT4dV9uVuujyJ3YanS8VL56FnuF8+mAI/hj7bGcsMvf/N3Y1vuLV S26oqbCjUG/DMMfNWoSElOjp9NOv8W77hXwbo+Fxwfi6S6AjmTO/avN/Yl+skRzkgIBW 1W+VpQrnH2/7e5kFyPPmzt8/EV2Nfh2YrrtDauFq/NQdPZENGfdW/wbtaP1hf2lywKtK 3Pae/m0a/ecfVUKdceFEbseWYYhlVAhqJlfg5IUTMhXa4h7zQfI3svsMtVyBGKYeT9AQ pdlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=JO6SipO1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q9-v6si5024744pll.370.2018.07.04.23.02.15; Wed, 04 Jul 2018 23:02:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=JO6SipO1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753460AbeGEGA2 (ORCPT + 99 others); Thu, 5 Jul 2018 02:00:28 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:52556 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753311AbeGEF7l (ORCPT ); Thu, 5 Jul 2018 01:59:41 -0400 Received: by mail-wm0-f65.google.com with SMTP id w16-v6so8886805wmc.2 for ; Wed, 04 Jul 2018 22:59:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5MVFn38qCec6lrkiQKmwFoZWAowYV/VmZ/hr9waL54M=; b=JO6SipO1H7sYGAP39wosc1YZvg4znbahCzNDd5yC6Sj4HkAFBx1ZZWNWpnwmo1WeuQ OW7XgfL6Ibs1q2nWs4MSIYrLRip3wY3f7+xWz2UDcXyY0YmnIayC1oSC4Cf8V7M5f9E9 zsfm9NuAWF3KSXMveKMLTkCdNeb2Mrp6kb2X3J5KkOkY3GH7nZk+yaIyE2cSZQr63AI+ XAUnn2d5d599+Yyd6lhh3RDEq5X0A+n3zYrXZ9AoBKlabG5kcyNYT4ooH18FNUwCXjog Eu2WxSElaxD5pOmL1N8m97g/obySfBXUUAgCsU0N2kEYViVCp/ry9vNdcnSNfiFVuIzj CB5Q== 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=5MVFn38qCec6lrkiQKmwFoZWAowYV/VmZ/hr9waL54M=; b=gqHRhDKQPRYYa8GbpeWjlNyt41Sm4kkf6RMCNx04c+UMQzv9i2HL+CJ/X1FWpeFkqH GTAmga63AzBQYPllUy871w7TO5d0fwRnbLsBLaWMRAcbcHuOTHfhHRjkrrNJW0b+FQKZ wBtCl+1YzqM+YPVJF80NMZz5IW5EeOkz/XwuM81/OvH3hMKw4lo+PFTx9VJPHzp+1d+L hTuM5BhkkqAZH+q5kYM6mVe+XgAsKmyeFlK/UiiVHwfM1JYGQ0AmqM8H96CzkY8Zy9H5 ExXE0W8N8SIgoTbFVItItgAvWB+n6Hk4v9OV0HFfBjxdzVKKyTh6lKHTsGPBTbvpLAvS +53w== X-Gm-Message-State: APt69E2/V+xQayA/H5ZKp3W9i3IFa+U/hbJJ10Z3Gg0QLKjRoGJm9/xM 6YEXIn2p1HJRIX2cNBHt45UsABNpvRfoTQ== X-Received: by 2002:a1c:cbc2:: with SMTP id b185-v6mr3272690wmg.117.1530770380126; Wed, 04 Jul 2018 22:59:40 -0700 (PDT) Received: from localhost.localdomain (p200300D993C4E0002E4725E865D6E031.dip0.t-ipconnect.de. [2003:d9:93c4:e000:2e47:25e8:65d6:e031]) by smtp.googlemail.com with ESMTPSA id h40-v6sm7452689wrf.40.2018.07.04.22.59.39 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 04 Jul 2018 22:59:39 -0700 (PDT) From: Manfred Spraul To: LKML , Davidlohr Bueso , Dmitry Vyukov Cc: 1vier1@web.de, Andrew Morton , Kees Cook , Manfred Spraul Subject: [PATCH 3/6] ipc/util.c: Use ipc_rcu_putref() for failues in ipc_addid() Date: Thu, 5 Jul 2018 07:59:17 +0200 Message-Id: <20180705055920.19611-4-manfred@colorfullife.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180705055920.19611-1-manfred@colorfullife.com> References: <20180705055920.19611-1-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ipc_addid() is impossible to use: - for certain failures, the caller must not use ipc_rcu_putref(), because the reference counter is not yet initialized. - for other failures, the caller must use ipc_rcu_putref(), because parallel operations could be ongoing already. The patch cleans that up, by initializing the refcount early, and by modifying all callers. The issues is related to the finding of syzbot+2827ef6b3385deb07eaf@syzkaller.appspotmail.com: syzbot found an issue with reading kern_ipc_perm.seq, here both read and write to already released memory could happen. Signed-off-by: Manfred Spraul Cc: Dmitry Vyukov Cc: Kees Cook Cc: Davidlohr Bueso --- ipc/msg.c | 2 +- ipc/sem.c | 2 +- ipc/shm.c | 2 ++ ipc/util.c | 12 ++++++++++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ipc/msg.c b/ipc/msg.c index 829c2062ded4..5bf5cb8017ea 100644 --- a/ipc/msg.c +++ b/ipc/msg.c @@ -162,7 +162,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params) /* ipc_addid() locks msq upon success. */ retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni); if (retval < 0) { - call_rcu(&msq->q_perm.rcu, msg_rcu_free); + ipc_rcu_putref(&msq->q_perm, msg_rcu_free); return retval; } diff --git a/ipc/sem.c b/ipc/sem.c index e8971fa1d847..9d49efeac2e5 100644 --- a/ipc/sem.c +++ b/ipc/sem.c @@ -556,7 +556,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params) /* ipc_addid() locks sma upon success. */ retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni); if (retval < 0) { - call_rcu(&sma->sem_perm.rcu, sem_rcu_free); + ipc_rcu_putref(&sma->sem_perm, sem_rcu_free); return retval; } ns->used_sems += nsems; diff --git a/ipc/shm.c b/ipc/shm.c index 59fe8b3b3794..06b7bf11a011 100644 --- a/ipc/shm.c +++ b/ipc/shm.c @@ -671,6 +671,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params) if (is_file_hugepages(file) && shp->mlock_user) user_shm_unlock(size, shp->mlock_user); fput(file); + ipc_rcu_putref(&shp->shm_perm, shm_rcu_free); + return error; no_file: call_rcu(&shp->shm_perm.rcu, shm_rcu_free); return error; diff --git a/ipc/util.c b/ipc/util.c index 662c28c6c9fa..8b09496ed720 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -248,7 +248,9 @@ static inline void ipc_set_seq(struct ipc_ids *ids, * Add an entry 'new' to the ipc ids idr. The permissions object is * initialised and the first free entry is set up and the id assigned * is returned. The 'new' entry is returned in a locked state on success. + * * On failure the entry is not locked and a negative err-code is returned. + * The caller must use ipc_rcu_putref() to free the identifier. * * Called with writer ipc_ids.rwsem held. */ @@ -258,6 +260,9 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) kgid_t egid; int id, err; + /* 1) Initialize the refcount so that ipc_rcu_putref works */ + refcount_set(&new->refcount, 1); + if (limit > IPCMNI) limit = IPCMNI; @@ -266,9 +271,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) idr_preload(GFP_KERNEL); - refcount_set(&new->refcount, 1); spin_lock_init(&new->lock); - new->deleted = false; rcu_read_lock(); spin_lock(&new->lock); @@ -277,6 +280,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) new->gid = new->cgid = egid; ipc_set_seq(ids, new); + new->deleted = false; /* * As soon as a new object is inserted into the idr, @@ -288,6 +292,9 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) * Thus the object must be fully initialized, and if something fails, * then the full tear-down sequence must be followed. * (i.e.: set new->deleted, reduce refcount, call_rcu()) + * + * This function sets new->deleted, the caller must use ipc_rcu_putef() + * for the remaining steps. */ id = ipc_idr_alloc(ids, new); idr_preload_end(); @@ -301,6 +308,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit) } } if (id < 0) { + new->deleted = true; spin_unlock(&new->lock); rcu_read_unlock(); return id; -- 2.17.1