From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36F98C433E0 for ; Mon, 15 Mar 2021 22:09:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A2D9B64F4D for ; Mon, 15 Mar 2021 22:09:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A2D9B64F4D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0BE2F6B0036; Mon, 15 Mar 2021 18:09:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 06ECF6B006C; Mon, 15 Mar 2021 18:09:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E03936B0070; Mon, 15 Mar 2021 18:09:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0137.hostedemail.com [216.40.44.137]) by kanga.kvack.org (Postfix) with ESMTP id C00906B0036 for ; Mon, 15 Mar 2021 18:09:24 -0400 (EDT) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 76DCB8249980 for ; Mon, 15 Mar 2021 22:09:24 +0000 (UTC) X-FDA: 77923500648.14.D628665 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf10.hostedemail.com (Postfix) with ESMTP id C5DE840F8C1F for ; Mon, 15 Mar 2021 22:03:01 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id b23so7457937pfo.8 for ; Mon, 15 Mar 2021 15:03:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pM/cP6/Lz3nYNB1A2BCPvDYZL5Nv+dU7p0YG5FmdOHs=; b=i+Pu5UNw/TgHu/fQEXmZOHFShdZNm0HYjuktOVvh43UQqWK9OXoxwTL0KfkLH1Lh3o Mkcv0D3zl4D39+93TZwtmfQnzTN3Fg40p8XufRexRcM6m8VQsn9YV4Kt6YovAUS/J0Qh oZNwlAoLttJ/w9GIdPY7BRwFC7DF52oYAgWR8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=pM/cP6/Lz3nYNB1A2BCPvDYZL5Nv+dU7p0YG5FmdOHs=; b=fy5xxKhZKae0GDjjiP0GSnyCf/AsicGWbk84dwrYOCeER995pYw7Ae49wzPSKtdfwy U5U8JCfVB/W7crgKUzMmJgvAWWWy3mmu311H/7BrETr4XFQhwKLA26gHNyei6kvEcHAQ aZzMxqe7Kvq4X/tFXp4BLnXn+KJW7ffF6XCkIms5ng5xp79ttbTIZkTHTdbXP7PkNj6E ihrfwDged/egjO6KEtlwJY7bqL1BmBqydOy5V+ZrbtcMdqu2xOSYmcSQQ1vbBuwtmL8W BHoQTCZEL5A6C4eMI0pg1EamoZVyMsaqwwIwanamDnulj7AOsApMrq+noDaGmGFHGWV3 WZ0w== X-Gm-Message-State: AOAM531qP2D3G9w5Y1Bvn25c4kOI7h71WfmXL13hTz5W8ardG84Xehhi xDG+4M45Z4BcahqAVxMiyc6JAQ== X-Google-Smtp-Source: ABdhPJy8q9F/S8HF2FiROpt7pJm13akaXdXmtXbP3bYKMQOOiCIYaDmmj4n4Edotx3q4QzWSMo2peA== X-Received: by 2002:a63:f808:: with SMTP id n8mr995720pgh.115.1615845780563; Mon, 15 Mar 2021 15:03:00 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h15sm14597755pfo.20.2021.03.15.15.02.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Mar 2021 15:02:59 -0700 (PDT) Date: Mon, 15 Mar 2021 15:02:58 -0700 From: Kees Cook To: Alexey Gladkov Cc: LKML , io-uring@vger.kernel.org, Kernel Hardening , Linux Containers , linux-mm@kvack.org, Alexey Gladkov , Andrew Morton , Christian Brauner , "Eric W . Biederman" , Jann Horn , Jens Axboe , Linus Torvalds , Oleg Nesterov Subject: Re: [PATCH v8 3/8] Use atomic_t for ucounts reference counting Message-ID: <202103151426.ED27141@keescook> References: <59ee3289194cd97d70085cce701bc494bfcb4fd2.1615372955.git.gladkov.alexey@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <59ee3289194cd97d70085cce701bc494bfcb4fd2.1615372955.git.gladkov.alexey@gmail.com> X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C5DE840F8C1F X-Stat-Signature: iibpg48rqipu6yak8p7fapds6bamobi9 Received-SPF: none (chromium.org>: No applicable sender policy available) receiver=imf10; identity=mailfrom; envelope-from=""; helo=mail-pf1-f174.google.com; client-ip=209.85.210.174 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615845781-977392 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Mar 10, 2021 at 01:01:28PM +0100, Alexey Gladkov wrote: > The current implementation of the ucounts reference counter requires the > use of spin_lock. We're going to use get_ucounts() in more performance > critical areas like a handling of RLIMIT_SIGPENDING. This really looks like it should be refcount_t. I read the earlier thread[1] on this, and it's not clear to me that this is a "normal" condition. I think there was a bug in that version (This appeared to *instantly* crash at boot with mnt_init() calling alloc_mnt_ns() calling inc_ucount()). The current code looks like just a "regular" reference counter of the allocated struct ucounts. Overflow should be very unexpected, yes? And operating on a "0" ucounts should be a bug too, right? > [...] > +/* 127: arbitrary random number, small enough to assemble well */ > +#define refcount_zero_or_close_to_overflow(ucounts) \ > + ((unsigned int) atomic_read(&ucounts->count) + 127u <= 127u) Regardless, this should absolutely not have "refcount" as a prefix. I realize it's only used here, but that's needlessly confusing with regard to it being atomic_t not refcount_t. > +struct ucounts *get_ucounts(struct ucounts *ucounts) > +{ > + if (ucounts) { > + if (refcount_zero_or_close_to_overflow(ucounts)) { > + WARN_ONCE(1, "ucounts: counter has reached its maximum value"); > + return NULL; > + } > + atomic_inc(&ucounts->count); > + } > + return ucounts; > +} I feel like this should just be: refcount_inc_not_zero(&ucounts->count); Or, to address Linus's comment in the v3 series, change get_ucounts to not return NULL first -- I can't see why that can ever happen in v8. -Kees [1] https://lore.kernel.org/lkml/116c7669744404364651e3b380db2d82bb23f983.1610722473.git.gladkov.alexey@gmail.com/ -- Kees Cook