Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3060268ybk; Mon, 18 May 2020 16:48:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw90ruyH6m7vW333MRsshHigMqq0CWqDnatj1CRLlZO+6yVNh7zRlTqAx8SsVMHuRxBEeOg X-Received: by 2002:a17:906:17c5:: with SMTP id u5mr17138117eje.275.1589845696377; Mon, 18 May 2020 16:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589845696; cv=none; d=google.com; s=arc-20160816; b=jL4N7VeERh2kqOdDM0YS8gD9yApMIwFJHs9CsfelGJe21zzNd0mSR8Bpdp1Gq/7xwL Osw0FBdOeC6CsHxPMxI1BH/I24h9zs00s4mgDn+omVLtDb4udvfGDEPxfhfWN0ihcFnw vggDThkIMxwlDh108mhoWLLmhVvcvkqh9rSCx1FpE4YOp/mKnSMEI3eqji4A/8Nk8vWC YcTrVansVDfBBRSpr+MDX2uxA1Pfdg+mIBjvLVgeJSnZScEn5gm6Ni2lvY18lgUfjlTw 44+07G285D1zqRf20LvZrOU9rZIS7RhneIYUoj5PMkaAe94ltp7mBC9I60l2DZX22tI1 9sbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=k5iYs4QQyLiioLyD2rJuWNuGTk1mgj+MGoMa7ymsagI=; b=vykiOyGkJIaTEL07PGYrgAQY0Sfnf+gw8JOrmLDPQ6gMrCKT7JnnwNQuVCLjI/+F0u QrYsCkb2/9Xq+2Xgw7mSWjH/beJuslZk4y+yOE+1NgTX7SVbSAg6pIpH8TCI1h5YUTcp uwLFfkeNT2RxHSuph+UqV/3Pu9ul/b/uQkXpDibYTkcmjpY65nvMDnosN/gL7HN0C8WO lwCWC1Wjf5kV9HaBSkNyV+LW2+QI1DMOl7nwszPaJKn1Smjux7wBFQdPupUn7bStKG8f y9+71Dpg/zK4tJNpb1NIzuUfI97IwSs9/Tcybu4xk76HEVcS/xwuZrIjf7dmvJxco5Ov dmJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HAa8B6qj; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i7si6221138edb.365.2020.05.18.16.47.54; Mon, 18 May 2020 16:48:16 -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=@kernel.org header.s=default header.b=HAa8B6qj; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731557AbgERSY2 (ORCPT + 99 others); Mon, 18 May 2020 14:24:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:48430 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730285AbgERRrx (ORCPT ); Mon, 18 May 2020 13:47:53 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7AFAB20657; Mon, 18 May 2020 17:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589824072; bh=TMrnDyP4MNmjV73iXl+Vaj/o8UvSmNm/5BIhilAenZE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HAa8B6qjp60l+kG78HRuCbIJGmjCTViLjlPs2w2bi98itYFbI6F9BWJYsBu4PULBY WxQCsR1cfxupg4iwVXIvk4f97li0cr1+YWVSZWQObcaK+fHUFOUdbetXXh/AsNTht3 zYSC3g2kqCxNyiIg8hhVOYpw/rPNeJNGCceh1ivw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andreas Schwab , Randy Dunlap , Vasily Averin , Andrew Morton , Waiman Long , NeilBrown , Steven Rostedt , Ingo Molnar , Peter Oberparleiter , Davidlohr Bueso , Manfred Spraul , Linus Torvalds , Sasha Levin Subject: [PATCH 4.14 060/114] ipc/util.c: sysvipc_find_ipc() incorrectly updates position index Date: Mon, 18 May 2020 19:36:32 +0200 Message-Id: <20200518173513.971628928@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518173503.033975649@linuxfoundation.org> References: <20200518173503.033975649@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vasily Averin [ Upstream commit 5e698222c70257d13ae0816720dde57c56f81e15 ] Commit 89163f93c6f9 ("ipc/util.c: sysvipc_find_ipc() should increase position index") is causing this bug (seen on 5.6.8): # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages # ipcmk -Q Message queue id: 0 # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages 0x82db8127 0 root 644 0 0 # ipcmk -Q Message queue id: 1 # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages 0x82db8127 0 root 644 0 0 0x76d1fb2a 1 root 644 0 0 # ipcrm -q 0 # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages 0x76d1fb2a 1 root 644 0 0 0x76d1fb2a 1 root 644 0 0 # ipcmk -Q Message queue id: 2 # ipcrm -q 2 # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages 0x76d1fb2a 1 root 644 0 0 0x76d1fb2a 1 root 644 0 0 # ipcmk -Q Message queue id: 3 # ipcrm -q 1 # ipcs -q ------ Message Queues -------- key msqid owner perms used-bytes messages 0x7c982867 3 root 644 0 0 0x7c982867 3 root 644 0 0 0x7c982867 3 root 644 0 0 0x7c982867 3 root 644 0 0 Whenever an IPC item with a low id is deleted, the items with higher ids are duplicated, as if filling a hole. new_pos should jump through hole of unused ids, pos can be updated inside "for" cycle. Fixes: 89163f93c6f9 ("ipc/util.c: sysvipc_find_ipc() should increase position index") Reported-by: Andreas Schwab Reported-by: Randy Dunlap Signed-off-by: Vasily Averin Signed-off-by: Andrew Morton Acked-by: Waiman Long Cc: NeilBrown Cc: Steven Rostedt Cc: Ingo Molnar Cc: Peter Oberparleiter Cc: Davidlohr Bueso Cc: Manfred Spraul Cc: Link: http://lkml.kernel.org/r/4921fe9b-9385-a2b4-1dc4-1099be6d2e39@virtuozzo.com Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- ipc/util.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ipc/util.c b/ipc/util.c index 7989f5e532198..5a65b0cbae7db 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -750,21 +750,21 @@ static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos, total++; } - *new_pos = pos + 1; + ipc = NULL; if (total >= ids->in_use) - return NULL; + goto out; for (; pos < IPCMNI; pos++) { ipc = idr_find(&ids->ipcs_idr, pos); if (ipc != NULL) { rcu_read_lock(); ipc_lock_object(ipc); - return ipc; + break; } } - - /* Out of range - return NULL to terminate iteration */ - return NULL; +out: + *new_pos = pos + 1; + return ipc; } static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) -- 2.20.1