From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751090AbdE2K4x (ORCPT ); Mon, 29 May 2017 06:56:53 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:35542 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750984AbdE2K4t (ORCPT ); Mon, 29 May 2017 06:56:49 -0400 From: ebiederm@xmission.com (Eric W. Biederman) To: Peter Zijlstra Cc: Christoph Hellwig , Kees Cook , Andrew Morton , Elena Reshetova , Greg KH , Ingo Molnar , Alexey Dobriyan , "Serge E. Hallyn" , arozansk@redhat.com, Davidlohr Bueso , Manfred Spraul , "axboe\@kernel.dk" , James Bottomley , "x86\@kernel.org" , Ingo Molnar , Arnd Bergmann , "David S. Miller" , Rik van Riel , linux-arch , "kernel-hardening\@lists.openwall.com" , LKML References: <1487590189-18151-1-git-send-email-elena.reshetova@intel.com> <20170303162352.b6af1c0c3115b3f5f1e7aed3@linux-foundation.org> <20170529083903.GA17735@infradead.org> <87h904xc26.fsf@xmission.com> <20170529102442.gerbzxzixllen46q@hirez.programming.kicks-ass.net> Date: Mon, 29 May 2017 05:49:53 -0500 In-Reply-To: <20170529102442.gerbzxzixllen46q@hirez.programming.kicks-ass.net> (Peter Zijlstra's message of "Mon, 29 May 2017 12:24:42 +0200") Message-ID: <87a85wvsxa.fsf@xmission.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1dFILg-0004if-S0;;;mid=<87a85wvsxa.fsf@xmission.com>;;;hst=in01.mta.xmission.com;;;ip=97.121.81.159;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/0EiA++08BIbYiBVVXb9Rehy+iJqvxpqA= X-SA-Exim-Connect-IP: 97.121.81.159 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.0 TVD_RCVD_IP Message was received from an IP address * 1.5 XMNoVowels Alpha-numberic number with no vowels * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa05 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa05 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;Peter Zijlstra X-Spam-Relay-Country: X-Spam-Timing: total 5552 ms - load_scoreonly_sql: 0.05 (0.0%), signal_user_changed: 3.7 (0.1%), b_tie_ro: 2.5 (0.0%), parse: 1.64 (0.0%), extract_message_metadata: 20 (0.4%), get_uri_detail_list: 3.4 (0.1%), tests_pri_-1000: 10 (0.2%), tests_pri_-950: 1.73 (0.0%), tests_pri_-900: 1.46 (0.0%), tests_pri_-400: 33 (0.6%), check_bayes: 32 (0.6%), b_tokenize: 14 (0.2%), b_tok_get_all: 9 (0.2%), b_comp_prob: 3.4 (0.1%), b_tok_touch_all: 3.3 (0.1%), b_finish: 0.73 (0.0%), tests_pri_0: 685 (12.3%), check_dkim_signature: 0.71 (0.0%), check_dkim_adsp: 3.9 (0.1%), tests_pri_500: 4790 (86.3%), poll_dns_idle: 4782 (86.1%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH 0/3] ipc subsystem refcounter conversions X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Peter Zijlstra writes: > On Mon, May 29, 2017 at 04:11:13AM -0500, Eric W. Biederman wrote: > >> Kees I I have a concern: >> >> __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r) >> { >> unsigned int new, val = atomic_read(&r->refs); >> >> do { >> if (!val) >> return false; >> >> if (unlikely(val == UINT_MAX)) >> return true; >> >> new = val + i; >> if (new < val) >> new = UINT_MAX; >> >> } while (!atomic_try_cmpxchg_relaxed(&r->refs, &val, new)); >> >> WARN_ONCE(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n"); >> >> return true; >> } >> >> Why in the world do you succeed when you the value saturates???? > > Why not? On saturation the object will leak and returning a reference to > it is always good. > >> From a code perspective that is bizarre. The code already has to handle >> the case when the counter does not increment. > > I don't see it as bizarre, we turned an overflow/use-after-free into a > leak. That's the primary mechanism here. > > As long as we have a reference to a leaked object, we might as well use > it, its not going anywhere. > >> Fixing the return value would move refcount_t into the realm of >> something that is desirable because it has bettern semantics and >> is more useful just on a day to day correctness point of view. Even >> ignoring the security implications. > > It changes the semantics between inc_not_zero() and inc(). It also > complicates the semantics of inc_not_zero(), where currently the failure > implies the count is 0 and means no-such-object, you complicate matters > by basically returning 'busy'. Busy is not a state of a reference count. It is true I am suggesting treating something with a saturated reference as not available. If that is what you mean by busy. But if it's reference is zero it is also not available. So there is no practical difference. > That is a completely new class of failure that is actually hard to deal > with, not to mention that it completely destroys refcount_inc_not_zero() > being a 'simple' replacement for atomic_inc_not_zero(). > > In case of the current failure, the no-such-object, we can fix that by > creating said object. But what to do on 'busy' ? Surely you don't want > to create another. You'd have to somehow retrofit something to wait on > in every user. Using little words. A return of true from inc_not_zero means we took a reference. A return of false means we did not take a reference. The code already handles I took a reference or I did not take a reference. Therefore lying with refcount_t is not helpful. It takes failures the code could easily handle and turns them into leaks. At least that is how I have seen reference counts used. And those are definitely the plane obivous semantics. Your changes are definitely not drop in replacements for atomic_t in my code. Eric