Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3834716ybf; Tue, 3 Mar 2020 13:50:27 -0800 (PST) X-Google-Smtp-Source: ADFU+vuErXQrFC3DN6XcNMWP6u7a9QKWItUeh/1t9z5aOQxdQSMTZ7/RauEH9Alify6pOWgxyF1l X-Received: by 2002:a05:6830:15d8:: with SMTP id j24mr5133695otr.258.1583272227123; Tue, 03 Mar 2020 13:50:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583272227; cv=none; d=google.com; s=arc-20160816; b=t6kw7vkDeVK4RzOucwLYlaHXhirwOOgt8khzZyylh9t42Yv5t096/5OjQkTx9ORIli o6HXrsOABzvLd/hXSvhccm7cyNRbGtyEu2WmG3TnM3SoFJLxC5+FK+h8HOqzZY/CEy06 wGmlHS0iGcoBJqEiVHmIvHB+12YDZOAZprfIxyJwZauNjdAmAJtJcgSYnHnFHwZbChvk m8HwO6hFQlQqfcYygXMJ7Jn8e99pa8pw3DERtTDMj+xMzs2wWwj7P+39R+/ESTH8/Z1J +YvguUhB2EhKi59VRP+jl2JrzkAoU/YhlmgoCUBLb/Lz4XE3mAWiLjYtu4ZVV1aliiQS LFag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=fwR7Sl83KeOsQT2W/DAwpFl0ZV6iw1v764tWAtDVZSg=; b=unl7bcv6Z/1AA7hU9EVYHc3C50qT3h8iTCQTEq8/aHndoY5hQKuE8HPwh5TxY4joeW IATj3iynJUw32jeTCehWr9XRYGIMuKafrx2+SzWb/TkInHPFXd/ju+hYgMNmHxSYbhzq DpareaeDQtEcDoHWM24hAjylzvrNkD354gh+26lrLIluwn19c70ovHXc+p1cTtw5+qXt 5pMIohm/vlIDUooFJQ4cMv/+D8PxIvC8rsw4AvqN1m0TGG4QM0BIvwBWK6twYm0QHu4k 8xQEUedhd3pZdTZ3rJnG68zyiZfjRMTzVtgO+0KqzuZ6kuUrdIe804aROASX+OriQCv4 em7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fOl9x86g; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l9si2528228otd.134.2020.03.03.13.50.15; Tue, 03 Mar 2020 13:50:27 -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=@chromium.org header.s=google header.b=fOl9x86g; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732365AbgCCVJ3 (ORCPT + 99 others); Tue, 3 Mar 2020 16:09:29 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:45499 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732274AbgCCVJ3 (ORCPT ); Tue, 3 Mar 2020 16:09:29 -0500 Received: by mail-pl1-f194.google.com with SMTP id b22so1881931pls.12 for ; Tue, 03 Mar 2020 13:09:28 -0800 (PST) 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=fwR7Sl83KeOsQT2W/DAwpFl0ZV6iw1v764tWAtDVZSg=; b=fOl9x86ga//EGU5D5JLMkKF+bt8LyTcxJBIWZ2dGr89Mpzz9sg46dz3Z9WXg5spuRf +5KkAM260XOqr4hFezfI0dAMkVHm2QrNAdvp27/d0QuCbfShXZU0mrxDKnCkuSp5mQ49 k9DQfKig4a/pE10r0ArWlycQSAwDX+FIKAyW8= 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=fwR7Sl83KeOsQT2W/DAwpFl0ZV6iw1v764tWAtDVZSg=; b=n+e28k8kQFR80oXSTxYCGUuGvztw5PqjQfJw0+wDpBRGwIOuL+cAudFNkDopjW8kmL pg69hS1JmuXUgLnFOAZEiBuXfa9eJtnAH/Ziixrf1GG/vLAvQ9KvYaYHCzjufOOgmjas vzxmnkX8i7vJBK84E8YRdQir0qcvfb6Q3AURosBe9o0Mwtq4BEvUvMNFidv6JhyExWrl zuJB4ZuzVLLkqCvjM60eBogeekH7utz1uVX1KWiXlvN3x/WbOuQOSzta2VuhgzmU4Mz8 haKq8npb0g7z8kjiKaTWmpsRkUAIpMool3t/T6CulPs2z0qMfzewKRUSta1Hl4Ob9Ytl n0wA== X-Gm-Message-State: ANhLgQ0q4+Nq/s5oARDzwVsqGK7f24MGtVwlx/CqDJxvD9TJOtm5zvnu TSJHu4L8Vry0sNIDhfr7Jfdxtg== X-Received: by 2002:a17:902:9a42:: with SMTP id x2mr6295193plv.194.1583269768021; Tue, 03 Mar 2020 13:09:28 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id e9sm132865pjt.16.2020.03.03.13.09.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 13:09:27 -0800 (PST) Date: Tue, 3 Mar 2020 13:09:26 -0800 From: Kees Cook To: Jann Horn Cc: Will Deacon , Ingo Molnar , Peter Zijlstra , kernel list , Elena Reshetova , Ard Biesheuvel , Hanjun Guo , Jan Glauber , Kernel Hardening Subject: Re: [PATCH v2] lib/refcount: Document interaction with PID_MAX_LIMIT Message-ID: <202003031309.FBE806C@keescook> References: <20200303105427.260620-1-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200303105427.260620-1-jannh@google.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 03, 2020 at 11:54:27AM +0100, Jann Horn wrote: > Document the circumstances under which refcount_t's saturation mechanism > works deterministically. > > Signed-off-by: Jann Horn Acked-by: Kees Cook Thanks! -Kees > > 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 > -- Kees Cook