Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4421976imw; Tue, 19 Jul 2022 06:24:27 -0700 (PDT) X-Google-Smtp-Source: AGRyM1teln7WX5zvwEWVoUg2gMGe4p31VUlXnLihvSwp6GwBB2eVg5n0ZgvjFhoGA25eetc26BLF X-Received: by 2002:a05:6402:51cb:b0:43a:e94c:a841 with SMTP id r11-20020a05640251cb00b0043ae94ca841mr43961562edd.212.1658237067425; Tue, 19 Jul 2022 06:24:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658237067; cv=none; d=google.com; s=arc-20160816; b=AL9G7k9qPHkKm8AaSkalayfmtmLcBxGcb5RKwAiFItnG8vXRWb4B0JM8uB8joiFANZ qWd4Pj246gYh8FYOSt0Kvv3VWYN74D8CYOghVnKQ1i03gdYWwSMpahP6QLIvtKrNYFH2 oMRN8J54Ow0cdXw5NdSe0fgpc4hBcMfa6lNcz9iM9CnjepFD5PgMkekp5YrjQERNS3Ln Yffu/B6KJpMdDqWtfZuATmPnlrifN4+oQsqbfwqYCG+e2f4bI7U7PVt1P+8uf7CFg++1 kbE6L2zA3ysoIBassVuB9wPizCOW0qtVOoK7rTPaD9hcURoPMvsfeCtCkXgPGqWGqm7d XUhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=YkcN0EH06Ks/0AAUnerlJKjCtaLpdHwTC3UiKKdQR7U=; b=S6wLzmaYd/y2pdk3GR8i9e1No0TfOiOWIO02wO0hqQOgnGYlNU5l5C4J2DVj6NtujE N57D6NyMcPVWFQVbSGwW88TDtmv2WjepnM+9PPAqj8Dgm5bJBm2wOI4XhJfDnUgpG3N9 tFbdqLYWopmcXnz3qHJe37OGiN4vRJAP641rcTW/3K9QHYYnnSyFu3KkrPSLAxg7cYMm AN4jva8MuAUHH+/EyqZb/jDPvLp/gLRDyDaECwW2RXe+I0CYs6AMHcphQHhmG6h10WiJ lQ0TNk6ef8YGHT/saFmucFU1YgC8Spfdp0EzQP95BlxkCt/ht72CuEttg4OjOXHe67jf 79hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HgR3DvUv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vj18-20020a170907131200b0072b8ce33a34si10479432ejb.180.2022.07.19.06.24.03; Tue, 19 Jul 2022 06:24:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HgR3DvUv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238678AbiGSMN1 (ORCPT + 99 others); Tue, 19 Jul 2022 08:13:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238925AbiGSMMi (ORCPT ); Tue, 19 Jul 2022 08:12:38 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6743D52DF9; Tue, 19 Jul 2022 05:03:51 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 7329ECE1BE4; Tue, 19 Jul 2022 12:03:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44D02C341C6; Tue, 19 Jul 2022 12:03:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658232227; bh=yPlDga9F1jLRX/iThlINi45gCIJJGIo7UG9swg1UtPM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HgR3DvUvjPYBi7XXnLb6KeuoEH6I3xeIzGQ431AWPFo4H9WO6XU0qdhkqJMIHufSj uQnVuSiO+MI45JOiE3iGopUyTMh4KOIi7L0NrHsUkn32OMADwa412i1ZzGPONnLUis EL9KC6EjAG8eRTjnwEhM6ENJNpl/Rqz4iuYO5/jA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kuniyuki Iwashima , Paul Moore , "David S. Miller" , Sasha Levin Subject: [PATCH 5.4 28/71] cipso: Fix data-races around sysctl. Date: Tue, 19 Jul 2022 13:53:51 +0200 Message-Id: <20220719114555.088364849@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220719114552.477018590@linuxfoundation.org> References: <20220719114552.477018590@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuniyuki Iwashima [ Upstream commit dd44f04b9214adb68ef5684ae87a81ba03632250 ] While reading cipso sysctl variables, they can be changed concurrently. So, we need to add READ_ONCE() to avoid data-races. Fixes: 446fda4f2682 ("[NetLabel]: CIPSOv4 engine") Signed-off-by: Kuniyuki Iwashima Acked-by: Paul Moore Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- Documentation/networking/ip-sysctl.txt | 2 +- net/ipv4/cipso_ipv4.c | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Documentation/networking/ip-sysctl.txt b/Documentation/networking/ip-sysctl.txt index 8af3771a3ebf..f60d4159fff4 100644 --- a/Documentation/networking/ip-sysctl.txt +++ b/Documentation/networking/ip-sysctl.txt @@ -876,7 +876,7 @@ cipso_cache_enable - BOOLEAN cipso_cache_bucket_size - INTEGER The CIPSO label cache consists of a fixed size hash table with each hash bucket containing a number of cache entries. This variable limits - the number of entries in each hash bucket; the larger the value the + the number of entries in each hash bucket; the larger the value is, the more CIPSO label mappings that can be cached. When the number of entries in a given hash bucket reaches this limit adding new entries causes the oldest entry in the bucket to be removed to make room. diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c index c1ac802d6894..42eaad5e515f 100644 --- a/net/ipv4/cipso_ipv4.c +++ b/net/ipv4/cipso_ipv4.c @@ -240,7 +240,7 @@ static int cipso_v4_cache_check(const unsigned char *key, struct cipso_v4_map_cache_entry *prev_entry = NULL; u32 hash; - if (!cipso_v4_cache_enabled) + if (!READ_ONCE(cipso_v4_cache_enabled)) return -ENOENT; hash = cipso_v4_map_cache_hash(key, key_len); @@ -297,13 +297,14 @@ static int cipso_v4_cache_check(const unsigned char *key, int cipso_v4_cache_add(const unsigned char *cipso_ptr, const struct netlbl_lsm_secattr *secattr) { + int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize); int ret_val = -EPERM; u32 bkt; struct cipso_v4_map_cache_entry *entry = NULL; struct cipso_v4_map_cache_entry *old_entry = NULL; u32 cipso_ptr_len; - if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0) + if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0) return 0; cipso_ptr_len = cipso_ptr[1]; @@ -323,7 +324,7 @@ int cipso_v4_cache_add(const unsigned char *cipso_ptr, bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1); spin_lock_bh(&cipso_v4_cache[bkt].lock); - if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) { + if (cipso_v4_cache[bkt].size < bkt_size) { list_add(&entry->list, &cipso_v4_cache[bkt].list); cipso_v4_cache[bkt].size += 1; } else { @@ -1200,7 +1201,8 @@ static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def, /* This will send packets using the "optimized" format when * possible as specified in section 3.4.2.6 of the * CIPSO draft. */ - if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10) + if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 && + ret_val <= 10) tag_len = 14; else tag_len = 4 + ret_val; @@ -1603,7 +1605,7 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option) * all the CIPSO validations here but it doesn't * really specify _exactly_ what we need to validate * ... so, just make it a sysctl tunable. */ - if (cipso_v4_rbm_strictvalid) { + if (READ_ONCE(cipso_v4_rbm_strictvalid)) { if (cipso_v4_map_lvl_valid(doi_def, tag[3]) < 0) { err_offset = opt_iter + 3; -- 2.35.1