Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9790435ybi; Wed, 24 Jul 2019 10:01:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3fmLO/nhFMTx9D15tT/2LqINHZhLTlCAb255eMjb6K9zHdLVt0eTRf0qs+4c08EzwKEXN X-Received: by 2002:a65:6288:: with SMTP id f8mr76892617pgv.292.1563987661548; Wed, 24 Jul 2019 10:01:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563987661; cv=none; d=google.com; s=arc-20160816; b=qQh7duL0aqjoOAX93VM/QKrDmdkSUX/9MpLvINqRViuzoNDBO+uFv3FbjxJB6C0ETm zWtpj3tr4tNICEd87VGsEMvjrK9Eb4TgqgGg781wu/hi6h7h4u2Bc0YJiizAYDg4Gg81 li9AI3A9ZEDQJHYDN+XtA1Uo+S3JehOHZ7QMgue/ZFneBJYMf2d/SCTpLWISUby1SR/Y EnsI1DlI8YYGyiQOzb055kr+yr9yp+y7RLT7UFLtM3WFss434hAHyTXTwvJkylCmNa8D p7EG3MrNNPhoPeGefFEOzYX/PgEYICePtzlj/iZ9MboBg+Sv4gSYZnZ2pTtMl/2DGm3x pydA== 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; bh=5IbouPeb+7HfRursxOx2UKgv+yR+yHqFG70l+YmYjuE=; b=NVV3378xKVk7kg2EE5wRDmwzVEqE7BEEtT4I4Y8DNXWTd9jTYI8tn9sLAOZ4Eg/ctK lUhz9g+bvRApuP9esE+cKN7l9tDuCJG2nJOxG7nQi5opBX8XOqCKqR/YYj84z983XkC5 pgKCry2d52BFBnstDN7RYKw8i6XPm6G3pc5vbwGHopIEQi0bEr5JXiTw+2bURDS5bnGY uOAOD8LJmteaB+Sd2w3r+I/mHVHZaENxcvd3dMNLc83Li2uuzk5kbcUBXCC7wzYiAhhA svxfFyGUBnACjnxZnIDmLGR3t0s7S8j0wrX2xKC5BgPKFPxcwha6ChCb9ukRC70BByeP YcKQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o3si18809561pld.102.2019.07.24.10.00.46; Wed, 24 Jul 2019 10:01:01 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728143AbfGXQRh (ORCPT + 99 others); Wed, 24 Jul 2019 12:17:37 -0400 Received: from mail-oi1-f196.google.com ([209.85.167.196]:40674 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727747AbfGXQRh (ORCPT ); Wed, 24 Jul 2019 12:17:37 -0400 Received: by mail-oi1-f196.google.com with SMTP id w196so14100661oie.7 for ; Wed, 24 Jul 2019 09:17:36 -0700 (PDT) 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=5IbouPeb+7HfRursxOx2UKgv+yR+yHqFG70l+YmYjuE=; b=S7zcAc+f9b417eohHPfm//8kk7zzB4uoXDMAfMqlYcXSiKP164dQwQP/5mHUq5idN1 yt1baL6mjDP6otV1hH4IalRVulYFviB6e89TdLmOFG2T392zTuuQeCNbV8XrBdd3QxEx cj590mTlaD0g+qQ8HTmUGDgH8RAi9lzIxpF0pxHfSoxOU4Zdo0yRitcGR8OxIoxebTAi Ob/5AGWZ3Dj2tpCJy5OH3A0a1+0X9MHrQYLEKIicDVXYsLk01R8Fth2SpxxODKi18w7p G4PHzgTTAPFHSFQ5GYfnIisZC/SLYFStSUI5CXm6VcnaN/YgoJf4GotFS4/gtHz+EfRl jrBw== X-Gm-Message-State: APjAAAUKqaWXZK5CD5NdYj4ebx/FaqdZgfgHnzM3/4/nw4LXLpI8dZG9 WPDMvC1YchjGEChe66JHae1NvZkKg3TyTaopcOu75w== X-Received: by 2002:aca:1c02:: with SMTP id c2mr42243691oic.166.1563985056283; Wed, 24 Jul 2019 09:17:36 -0700 (PDT) MIME-Version: 1.0 References: <20190722113151.1584-1-nitin.r.gote@intel.com> In-Reply-To: From: Ondrej Mosnacek Date: Wed, 24 Jul 2019 18:17:27 +0200 Message-ID: Subject: Re: [PATCH] selinux: convert struct sidtab count to refcount_t To: Jann Horn Cc: NitinGote , Kees Cook , Kernel Hardening , Paul Moore , Stephen Smalley , Eric Paris , SElinux list , Linux kernel mailing list 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, Jul 23, 2019 at 4:54 PM Jann Horn wrote: > On Mon, Jul 22, 2019 at 3:44 PM Ondrej Mosnacek wrote: > > On Mon, Jul 22, 2019 at 1:35 PM NitinGote wrote: > > > refcount_t type and corresponding API should be > > > used instead of atomic_t when the variable is used as > > > a reference counter. This allows to avoid accidental > > > refcounter overflows that might lead to use-after-free > > > situations. > > > > > > Signed-off-by: NitinGote > > > > Nack. > > > > The 'count' variable is not used as a reference counter here. It > > tracks the number of entries in sidtab, which is a very specific > > lookup table that can only grow (the count never decreases). I only > > made it atomic because the variable is read outside of the sidtab's > > spin lock and thus the reads and writes to it need to be guaranteed to > > be atomic. The counter is only updated under the spin lock, so > > insertions do not race with each other. > > Probably shouldn't even be atomic_t... quoting Documentation/atomic_t.txt: > > | SEMANTICS > | --------- > | > | Non-RMW ops: > | > | The non-RMW ops are (typically) regular LOADs and STOREs and are canonically > | implemented using READ_ONCE(), WRITE_ONCE(), smp_load_acquire() and > | smp_store_release() respectively. Therefore, if you find yourself only using > | the Non-RMW operations of atomic_t, you do not in fact need atomic_t at all > | and are doing it wrong. > > So I think what you actually want here is a plain "int count", and then: > - for unlocked reads, either READ_ONCE()+smp_rmb() or smp_load_acquire() > - for writes, either smp_wmb()+WRITE_ONCE() or smp_store_release() > > smp_load_acquire() and smp_store_release() are probably the nicest > here, since they are semantically clearer than smp_rmb()/smp_wmb(). Oh yes, I had a hunch that there would be a better way to do it... I should have taken the time to read the documentation carefully :) I am on PTO today, but I will be happy to send a patch to convert the atomic_t usage to the smp_load_acquire()/smp_store_release() helpers tomorrow. It will also allow us to just use u32 directly and to get rid of the ugly casts and the INT_MAX limit. Thanks a lot for the hint, Jann! -- Ondrej Mosnacek Software Engineer, Security Technologies Red Hat, Inc.