Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp707029pxb; Tue, 5 Apr 2022 19:39:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjqbm1ww0U0d8f7ekEizugRGpm3y2f1Ajh83unG5VsYGczEi9MU3xFafx8SkU7oM/JnGQP X-Received: by 2002:a63:3e01:0:b0:386:3916:182f with SMTP id l1-20020a633e01000000b003863916182fmr5267152pga.435.1649212758729; Tue, 05 Apr 2022 19:39:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649212758; cv=none; d=google.com; s=arc-20160816; b=lZEMvS83YgPtq7zFsXFrEnAENl+xpjq2BzlvFoT3sSoB8MOMA25hbl1H+za2T1i31k qxBU4FrXztoNA0+mbmr5Z/uMDvHmJl/6w8nYYeyLaMbInPCuS+sQ7McBVpil22AmLFVD sqA1oFZg1ZczK3yQQWJpqE3Mh7WDs7mUz4WrXlrgHGQ2Echgp65CdlPQhbEIw4BLxg5p mzzYvPhzzsFrlkl8DBQAAi1alAJs4qJzkPgiO+fJ++qqMoGE7XBTYyGoJR77ApVfKWhE cd3hTUoEzyE1dcIvojOH7NMqQu3yOcKZYSNPSoV2Yx6/yWxYK+TUucT2r9JLcwVTwqCZ wj3A== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SGEnGLYB8xUgR5i2f8oVftVOJmP1ltsmgURX2HHhUjY=; b=aS8+jZ88RloOol491yEd+YrqKtbVrW7FUTm03E8iiJWQAuBrjlLeDGB8usAK1nEBAl wDnbKbkppL+d3a+9ToRX9rJb0LI5lAQRiyE4lJXIxj//LxOjtuk91ckILP389PnweRg1 uErnRKAXxk39UgbNnBdBUdXCrM5gbLr4dRllFHpzHvzQKCUCvKg2zQTQI/P6y/VzJemN WSsvJu5Ut3AZSbn3+GzWKoJxT1KPKLAcA+0c6PIYiXlDlbKtCBJEpDCb2BLwVdnLF8z2 uyQbzDdLfxqgvnKHvzpSNFWKOIZkyYiX+gXQVWru5HXeB2pcP0Xfyzrire/1mElEqKHn AbHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zsJkViU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cl10-20020a056a0032ca00b004fa3a8e00aesi14820921pfb.357.2022.04.05.19.39.04; Tue, 05 Apr 2022 19:39:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zsJkViU4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384461AbiDEOSG (ORCPT + 99 others); Tue, 5 Apr 2022 10:18:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239002AbiDEJdP (ORCPT ); Tue, 5 Apr 2022 05:33:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA77AAE76; Tue, 5 Apr 2022 02:21:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7655B61659; Tue, 5 Apr 2022 09:21:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 785F6C385A2; Tue, 5 Apr 2022 09:21:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150484; bh=GI69kqRZ/96tdDLJ7TS13wmPucNv5WoL1rV8SBNQJ00=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zsJkViU49TlXaCs0rCheEHZCaYhfeHBW/hnWN6x63hBbwxvGgEMTdOmvOOV7OYqAr MErQOL10/cndXoA+1Aa4XBvu1IK6h+5Qbg+IHdzscM/N01YqsrYHtb3DReyhnwyEtm U9E7itOgSmktWRHZ1boLMx5FIhpwx0QBeHIw20ec= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , "Eric W. Biederman" Subject: [PATCH 5.15 071/913] mm/mlock: fix two bugs in user_shm_lock() Date: Tue, 5 Apr 2022 09:18:53 +0200 Message-Id: <20220405070341.952843038@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miaohe Lin commit e97824ff663ce3509fe040431c713182c2f058b1 upstream. user_shm_lock forgets to set allowed to 0 when get_ucounts fails. So the later user_shm_unlock might do the extra dec_rlimit_ucounts. Also in the RLIM_INFINITY case, user_shm_lock will success regardless of the value of memlock where memblock == LONG_MAX && !capable(CAP_IPC_LOCK) should fail. Fix all of these by changing the code to leave lock_limit at ULONG_MAX aka RLIM_INFINITY, leave "allowed" initialized to 0 and remove the special case of RLIM_INFINITY as nothing can be greater than ULONG_MAX. Credit goes to Eric W. Biederman for proposing simplifying the code and thus catching the later bug. Fixes: d7c9e99aee48 ("Reimplement RLIMIT_MEMLOCK on top of ucounts") Signed-off-by: Miaohe Lin Cc: stable@vger.kernel.org v1: https://lkml.kernel.org/r/20220310132417.41189-1-linmiaohe@huawei.com v2: https://lkml.kernel.org/r/20220314064039.62972-1-linmiaohe@huawei.com Link: https://lkml.kernel.org/r/20220322080918.59861-1-linmiaohe@huawei.com Signed-off-by: Eric W. Biederman Signed-off-by: Greg Kroah-Hartman --- mm/mlock.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) --- a/mm/mlock.c +++ b/mm/mlock.c @@ -826,13 +826,12 @@ int user_shm_lock(size_t size, struct uc locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; lock_limit = rlimit(RLIMIT_MEMLOCK); - if (lock_limit == RLIM_INFINITY) - allowed = 1; - lock_limit >>= PAGE_SHIFT; + if (lock_limit != RLIM_INFINITY) + lock_limit >>= PAGE_SHIFT; spin_lock(&shmlock_user_lock); memlock = inc_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked); - if (!allowed && (memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) { + if ((memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) { dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked); goto out; }