Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2927427ybk; Mon, 18 May 2020 11:18:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyl9DvvmZixRdTdUcDJOUrFkzyXXKffnlaT9T42jYQ7KpTzi6k6GJ1U5iiqq9jMYoN9/ZRZ X-Received: by 2002:a50:c3c2:: with SMTP id i2mr14458707edf.93.1589825892386; Mon, 18 May 2020 11:18:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589825892; cv=none; d=google.com; s=arc-20160816; b=LaQgHx5QRKPeysSKZ7lEsm/D5bOToomDBoVzwU4PBeD3luw22OcDwn1cAWs+7+lmIA cQkZL95WjoXvbgRjmmbsLdzjj8BPvLU5s237ANIG9ILh+OoN6aJ6Fd6vGX71gvnqE+tR 2gpHtjnJcxcswprr1W2cl9h8bWBoJVXK6zA6YM88meAp0Me3OxLWWO42T7g52ec4p0Se l35k6puNCNgJEIAmDrgIvggnNoqKbjjGneXKVok7lVTnEhct9WCrd8l4DQGs9E0BJRl0 U6MuTfomGlEqWWtRyhCkX0b/cKS+suvRv+rwe2EEpdp5E3sG2VwQ1g2YNW9KkzXb4qKB 1bCQ== 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=zWQHLZM+JpKFtc2gWDy1UqlaMam1P9IwDfKtTu6i144=; b=ekPzZS72U0WMQm/CaLxAKcSaVorP6ynh9jPQa+PM/y9yq7WyH9JyuX8PlXVMiV6Qin LB8etNw7Un5KrrT/1LMkTIodf8LmbgKzVilhq+qCqypmweloHFGlrNyTEo8HOdPOpbDW DmrOf97hovgbfwos8Pz9W4MB/TQbFPfnIcdoHgJtnSHwsQXChU2Ix87p68jsOQAcIl6O M6g0A7lJGXbB/82iJeHbqi6c2YHlhbVVmEQVgzpiDlnb9mkUsCaW1hUn8JQ3KMxV7yqb bWjmq/HsjwMA9lH/RDCNJ0LItDBYsR+HwRQvYGUAi43PUaoyy9eqqkPA0JN0qfYuk0tL 4xDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SdAAK7zk; 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 i7si2378738edf.109.2020.05.18.11.17.48; Mon, 18 May 2020 11:18:12 -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=SdAAK7zk; 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 S2387617AbgERSND (ORCPT + 99 others); Mon, 18 May 2020 14:13:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:46858 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731456AbgERSCe (ORCPT ); Mon, 18 May 2020 14:02:34 -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 867A220853; Mon, 18 May 2020 18:02:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589824954; bh=DjJuFW+jA4uFjcdhZu8ZSi53gVyWHEuAKH+nDl3oFMA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SdAAK7zkf+BTA6Gpsn7imEKBGalwVfFVDk2+QVm9vXbdqO2v3ZB6qwCKZqdQgJJCC 3KhXnsgmRAzWa1JwWE0wb6NGH6fSdMFK2XKGRwvA4PehSWKvKAwytQ3wVcMCuPikWy f9pSl3j4sYTKpXvQuLcVHiTdyXpOKcrPPrDoFZNQ= 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 5.6 069/194] ipc/util.c: sysvipc_find_ipc() incorrectly updates position index Date: Mon, 18 May 2020 19:35:59 +0200 Message-Id: <20200518173537.509476839@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518173531.455604187@linuxfoundation.org> References: <20200518173531.455604187@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 2d70f25f64b83..c4a67982ec008 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -764,21 +764,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 < ipc_mni; 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