Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3387988ybf; Tue, 3 Mar 2020 05:10:49 -0800 (PST) X-Google-Smtp-Source: ADFU+vt08ArbHlWjHgWuwQNqejD3aDWyn2Bg6b6NhxOp67Ktc2WZxiq7TdDByxWSMzQyHj47p+EL X-Received: by 2002:a05:6808:10b:: with SMTP id b11mr2459264oie.110.1583241049794; Tue, 03 Mar 2020 05:10:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583241049; cv=none; d=google.com; s=arc-20160816; b=1GPghRfD26S+g+eVaNXiSkxn6Cpn7FCXIe+HC6FCnFVjr3VFxWMmABL+cD7PQzoByB GnrTTkZ2gqyNRmixNt8CcP5SE1BsNPxd9qZ+sp4k2kJtwRG9iOyzKakG1EUl0WC9k1DY a+r3STAmIhyg092zxRkBmIAJADdYvUacH7q+xfY3DjZ29kmyeNrXBDEVZsBgxiI/cy/D c/B3TPiHqJXSSumAaD8vX535XGKAU5aSdMzD5V0LynuecgWxIZiUUw/PJW+8S7AJjrgg iGyO2RY1jaIY1Vhr8O0nHBck27h0bWNels7svRBnfNqZgm5KG2zPMDK+8B1ZN3MSNAiP /4/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vS+hCXJAKRmx8Dc5B7DWkwttpDrd7EUzO0LUB6S40pg=; b=pv+sroplyvp/Ysgi+/jlePIbIbrT+lr9qdhvBi3j+1jIY2ukgDQzRrbe5WUJpCbFZQ qtmp81V5xGOHy0ZUowZsA1AAjvTta/FgpWfLA60z5xof5sIkqzHV8W+2S3b/Ddl7M4TH ju1OWmixHrF1WJwYWHTU2q/eygTvQfESMwznfTVdA/DCt7Vh8z7ZvKg07M6zgO1ERMgq gGswSLc6WJsas3jkiFVPtyNkMZjLgyG1xVzI1dznrBSWibVJHZQtSoTO+IQYldgb/v12 MqaqRuAP5Yih0aIx+nw67xoM9w5jRQHxZnu3YOs3dJIZqPFnBLnWboEoF/aTwJ5EaMCq 3YCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=y7yyoZEi; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z23si7987270oti.34.2020.03.03.05.10.34; Tue, 03 Mar 2020 05:10:49 -0800 (PST) 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=@linaro.org header.s=google header.b=y7yyoZEi; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729299AbgCCNHY (ORCPT + 99 others); Tue, 3 Mar 2020 08:07:24 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:51248 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728141AbgCCNHX (ORCPT ); Tue, 3 Mar 2020 08:07:23 -0500 Received: by mail-wm1-f65.google.com with SMTP id a132so3105169wme.1 for ; Tue, 03 Mar 2020 05:07:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vS+hCXJAKRmx8Dc5B7DWkwttpDrd7EUzO0LUB6S40pg=; b=y7yyoZEittWnefLa9L+2ZrRgX4Tn0/G3wmR6sXtwtEYMlGF8xl8PFwuDjvsIX6ryLU IVskjhMSWt4dW2Yu/MVc97aQ2kBHkWCqSRboKvgIL9dk9Iewks1cQIRxBpDtAbZ4vxh/ Xaw9Ua2N7tl6NEm0VuEKFZzHz8Uj4Kg4FWBEY0+YIBUor7PdCWqpShCefsEqQKPT9oms DDJAv2kfS/Z6gDtezlNfggGwgQxcDFzMyjUrt/ZiEGZTyvAzmZIV1yfy4OgwDxxRAByJ cWMqdU0/dmXzTRkXWbtn1r6ZcP+9CjwNQT1Ia9X4KxInckq0H5rBMtpVhXLgWb3+ZneE 9uJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vS+hCXJAKRmx8Dc5B7DWkwttpDrd7EUzO0LUB6S40pg=; b=QV1bC9ChYzaEdE8NC0eavoFCOoIv6hnwDhlaoYnvTVRZTa7FrksIpLPApIN2+NoO9/ mV/VLNXONvFBK1folgGlOavGbPAz4YyY97KtKnGwGLFFVX5cLBZyZiYxbYonGfRzVPwY peWejNRZ8rGCL3UStpwsvay5Sd6Fz1sbmh3MNBcpgbq6ELcyj5o7iksZeCJ6d3oOPCe7 OqK5qzS3q1DuK6doxf4s/TG/gfi86asrJk8IlVnGzsUAM2ffcsjRrJv78UYiu7ISQRJj jhRqHjuxncwHbWDcg7qdR2dwaRfqzoxvxGWp0DmurYCmf5Vs6L15VtUMmSgJpbqZa4+y IP8Q== X-Gm-Message-State: ANhLgQ1/SnJBejKgde4u1/LrxKL5xv8Tyj1uYl4bQvFxWHoBmY14biw6 bxE2I9ghOVVPbVL7xEDV4kF7FDbeJKUCSP5sClT9Ait63fPdtw== X-Received: by 2002:a1c:2d88:: with SMTP id t130mr4588112wmt.68.1583240842267; Tue, 03 Mar 2020 05:07:22 -0800 (PST) MIME-Version: 1.0 References: <20200303105427.260620-1-jannh@google.com> In-Reply-To: <20200303105427.260620-1-jannh@google.com> From: Ard Biesheuvel Date: Tue, 3 Mar 2020 14:07:11 +0100 Message-ID: Subject: Re: [PATCH v2] lib/refcount: Document interaction with PID_MAX_LIMIT To: Jann Horn Cc: Will Deacon , Kees Cook , Ingo Molnar , Peter Zijlstra , kernel list , Elena Reshetova , Hanjun Guo , Jan Glauber , Kernel Hardening Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 3 Mar 2020 at 11:54, Jann Horn wrote: > > Document the circumstances under which refcount_t's saturation mechanism > works deterministically. > > Signed-off-by: Jann Horn I /think/ the main point of Kees's suggestion was that FUTEX_TID_MASK is UAPI, so unlikely to change. > --- > > Notes: > v2: > - write down the math (Kees) > > include/linux/refcount.h | 23 ++++++++++++++++++----- > 1 file changed, 18 insertions(+), 5 deletions(-) > > diff --git a/include/linux/refcount.h b/include/linux/refcount.h > index 0ac50cf62d062..0e3ee25eb156a 100644 > --- a/include/linux/refcount.h > +++ b/include/linux/refcount.h > @@ -38,11 +38,24 @@ > * atomic operations, then the count will continue to edge closer to 0. If it > * reaches a value of 1 before /any/ of the threads reset it to the saturated > * value, then a concurrent refcount_dec_and_test() may erroneously free the > - * underlying object. Given the precise timing details involved with the > - * round-robin scheduling of each thread manipulating the refcount and the need > - * to hit the race multiple times in succession, there doesn't appear to be a > - * practical avenue of attack even if using refcount_add() operations with > - * larger increments. > + * underlying object. > + * Linux limits the maximum number of tasks to PID_MAX_LIMIT, which is currently > + * 0x400000 (and can't easily be raised in the future beyond FUTEX_TID_MASK). > + * With the current PID limit, if no batched refcounting operations are used and > + * the attacker can't repeatedly trigger kernel oopses in the middle of refcount > + * operations, this makes it impossible for a saturated refcount to leave the > + * saturation range, even if it is possible for multiple uses of the same > + * refcount to nest in the context of a single task: > + * > + * (UINT_MAX+1-REFCOUNT_SATURATED) / PID_MAX_LIMIT = > + * 0x40000000 / 0x400000 = 0x100 = 256 > + * > + * If hundreds of references are added/removed with a single refcounting > + * operation, it may potentially be possible to leave the saturation range; but > + * given the precise timing details involved with the round-robin scheduling of > + * each thread manipulating the refcount and the need to hit the race multiple > + * times in succession, there doesn't appear to be a practical avenue of attack > + * even if using refcount_add() operations with larger increments. > * > * Memory ordering > * =============== > > base-commit: 98d54f81e36ba3bf92172791eba5ca5bd813989b > -- > 2.25.0.265.gbab2e86ba0-goog >