From patchwork Tue Mar 30 13:16:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 12172553 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3E6BC433C1 for ; Tue, 30 Mar 2021 13:17:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8396F61969 for ; Tue, 30 Mar 2021 13:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231622AbhC3NRB (ORCPT ); Tue, 30 Mar 2021 09:17:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:36936 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232119AbhC3NQy (ORCPT ); Tue, 30 Mar 2021 09:16:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617110214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=K7XcRY5QAtM+y7zJAE5m+E8fGUGxe1uqwiXVWbv4u/w=; b=CnqI30mRgSYdCYSUnpYys0MwVtCv7/OU9M/d7SfmYl5Iq4PwlOUOiXxI0H3hdSokrRXEJ7 Z4g4iEw6ZEvVAGaCOOMU6KlC4jBsmfbmwX68Yg42XzSkCXeOwfOBoMvUUcq+lUpzumS97u Xrdtao0YA0aNbP08+kagtSmk6lHpffk= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-66-oes3AwNCPO-foNqgMiyG9Q-1; Tue, 30 Mar 2021 09:16:51 -0400 X-MC-Unique: oes3AwNCPO-foNqgMiyG9Q-1 Received: by mail-ej1-f71.google.com with SMTP id d6so7127259ejd.15 for ; Tue, 30 Mar 2021 06:16:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K7XcRY5QAtM+y7zJAE5m+E8fGUGxe1uqwiXVWbv4u/w=; b=ppVTwT6Pfr28nMQoYliozS4fT+USzYUbzUWeYLnkzdogmcnwp17H3NVEFxD7skcG50 yRX7688JF9Bq/5lKUgptOjtJfoxPyc+ViB+s0tZmnY7TpDPuSUb+proZ9kopnMZ6JGtE qU4t3u9MN4Pem1I9fIjYAHeF7AWAra++hduT2BT47+hnERnKjegumSQLwvMBsVf9x68C OkHor4TJtzn9q+XYR8PRQnP1tBicG2ni9AdyerXuyzlz1L8GqIVcodMsBY/Jgd5e48dS SMP63ptwkOZCWWBZrZT/Kd2qCAKBUUwOLMRlpPhVmaKkR4O+l3dqeO5b+Q1ATp5sVr/V ITQw== X-Gm-Message-State: AOAM530IDPZL5NiQxleoGv6USm8lZmu3NwQ8U72ubODp9uCxzNO9MOHW Cf3Kx+6m8OvDoxDRfs9a3RWomptwEGGyTWRsfADP3vguF+1SojMVUk9KWerKnp1SAomOjjoP+vJ sBWYlYyaCeAq+3jjpk0ZUOiwGpIxeNJAV1W8kAxlp5RG+pnz2gfggqBdtbNFpbJQxlr3cLg== X-Received: by 2002:a17:906:4e57:: with SMTP id g23mr33201924ejw.47.1617110210012; Tue, 30 Mar 2021 06:16:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz114D5KBci8LXhPcLe80INnY6Y1JpZ3p2WYdcsUbDU3ws1i29FUJm//zP+I8OZnRKTm+bR0g== X-Received: by 2002:a17:906:4e57:: with SMTP id g23mr33201905ejw.47.1617110209762; Tue, 30 Mar 2021 06:16:49 -0700 (PDT) Received: from localhost.localdomain ([2a02:8308:b105:dd00:277b:6436:24db:9466]) by smtp.gmail.com with ESMTPSA id u16sm11178245edq.4.2021.03.30.06.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 06:16:49 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH 1/3] selinux: fix cond_list corruption when changing booleans Date: Tue, 30 Mar 2021 15:16:44 +0200 Message-Id: <20210330131646.1401838-2-omosnace@redhat.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210330131646.1401838-1-omosnace@redhat.com> References: <20210330131646.1401838-1-omosnace@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org Currently, duplicate_policydb_cond_list() first copies the whole conditional avtab and then tries to link to the correct entries in cond_dup_av_list() using avtab_search(). However, since the conditional avtab may contain multiple entries with the same key, this approach often fails to find the right entry, potentially leading to wrong rules being activated/deactivated when booleans are changed. To fix this, instead start with an empty conditional avtab and add the individual entries one-by-one while building the new av_lists. This approach leads to the correct result, since each entry is present in the av_lists exactly once. The issue can be reproduced with Fedora policy as follows: # sesearch -s ftpd_t -t public_content_rw_t -c dir -p create -A allow ftpd_t non_security_file_type:dir { add_name create getattr ioctl link lock open read remove_name rename reparent rmdir search setattr unlink watch watch_reads write }; [ ftpd_full_access ]:True allow ftpd_t public_content_rw_t:dir { add_name create link remove_name rename reparent rmdir setattr unlink watch watch_reads write }; [ ftpd_anon_write ]:True # setsebool ftpd_anon_write=off ftpd_connect_all_unreserved=off ftpd_connect_db=off ftpd_full_access=off On fixed kernels, the sesearch output is the same after the setsebool command: # sesearch -s ftpd_t -t public_content_rw_t -c dir -p create -A allow ftpd_t non_security_file_type:dir { add_name create getattr ioctl link lock open read remove_name rename reparent rmdir search setattr unlink watch watch_reads write }; [ ftpd_full_access ]:True allow ftpd_t public_content_rw_t:dir { add_name create link remove_name rename reparent rmdir setattr unlink watch watch_reads write }; [ ftpd_anon_write ]:True While on the broken kernels, it will be different: # sesearch -s ftpd_t -t public_content_rw_t -c dir -p create -A allow ftpd_t non_security_file_type:dir { add_name create getattr ioctl link lock open read remove_name rename reparent rmdir search setattr unlink watch watch_reads write }; [ ftpd_full_access ]:True allow ftpd_t non_security_file_type:dir { add_name create getattr ioctl link lock open read remove_name rename reparent rmdir search setattr unlink watch watch_reads write }; [ ftpd_full_access ]:True allow ftpd_t non_security_file_type:dir { add_name create getattr ioctl link lock open read remove_name rename reparent rmdir search setattr unlink watch watch_reads write }; [ ftpd_full_access ]:True Fixes: c7c556f1e81b ("selinux: refactor changing booleans") Signed-off-by: Ondrej Mosnacek --- security/selinux/ss/avtab.c | 90 +++++++++---------------------- security/selinux/ss/avtab.h | 2 +- security/selinux/ss/conditional.c | 12 ++--- 3 files changed, 33 insertions(+), 71 deletions(-) diff --git a/security/selinux/ss/avtab.c b/security/selinux/ss/avtab.c index 6dcb6aa4db7f..11f8f524de98 100644 --- a/security/selinux/ss/avtab.c +++ b/security/selinux/ss/avtab.c @@ -305,86 +305,48 @@ void avtab_init(struct avtab *h) h->nel = 0; } -int avtab_alloc(struct avtab *h, u32 nrules) +static int avtab_alloc_common(struct avtab *h, u32 nslot) { - u32 mask = 0; - u32 shift = 0; - u32 work = nrules; - u32 nslot = 0; - - if (nrules == 0) - goto avtab_alloc_out; - - while (work) { - work = work >> 1; - shift++; - } - if (shift > 2) - shift = shift - 2; - nslot = 1 << shift; - if (nslot > MAX_AVTAB_HASH_BUCKETS) - nslot = MAX_AVTAB_HASH_BUCKETS; - mask = nslot - 1; - h->htable = kvcalloc(nslot, sizeof(void *), GFP_KERNEL); if (!h->htable) return -ENOMEM; - avtab_alloc_out: h->nel = 0; h->nslot = nslot; - h->mask = mask; - pr_debug("SELinux: %d avtab hash slots, %d rules.\n", - h->nslot, nrules); + h->mask = nslot - 1; return 0; } -int avtab_duplicate(struct avtab *new, struct avtab *orig) +int avtab_alloc(struct avtab *h, u32 nrules) { - int i; - struct avtab_node *node, *tmp, *tail; - - memset(new, 0, sizeof(*new)); + int rc; + u32 shift = 0; + u32 work = nrules; + u32 nslot = 0; - new->htable = kvcalloc(orig->nslot, sizeof(void *), GFP_KERNEL); - if (!new->htable) - return -ENOMEM; - new->nslot = orig->nslot; - new->mask = orig->mask; - - for (i = 0; i < orig->nslot; i++) { - tail = NULL; - for (node = orig->htable[i]; node; node = node->next) { - tmp = kmem_cache_zalloc(avtab_node_cachep, GFP_KERNEL); - if (!tmp) - goto error; - tmp->key = node->key; - if (tmp->key.specified & AVTAB_XPERMS) { - tmp->datum.u.xperms = - kmem_cache_zalloc(avtab_xperms_cachep, - GFP_KERNEL); - if (!tmp->datum.u.xperms) { - kmem_cache_free(avtab_node_cachep, tmp); - goto error; - } - tmp->datum.u.xperms = node->datum.u.xperms; - } else - tmp->datum.u.data = node->datum.u.data; - - if (tail) - tail->next = tmp; - else - new->htable[i] = tmp; - - tail = tmp; - new->nel++; + if (nrules != 0) { + while (work) { + work = work >> 1; + shift++; } + if (shift > 2) + shift = shift - 2; + nslot = 1 << shift; + if (nslot > MAX_AVTAB_HASH_BUCKETS) + nslot = MAX_AVTAB_HASH_BUCKETS; } + rc = avtab_alloc_common(h, nslot); + if (rc) + return rc; + + pr_debug("SELinux: %d avtab hash slots, %d rules.\n", nslot, nrules); return 0; -error: - avtab_destroy(new); - return -ENOMEM; +} + +int avtab_alloc_dup(struct avtab *new, const struct avtab *orig) +{ + return avtab_alloc_common(new, orig->nslot); } void avtab_hash_eval(struct avtab *h, char *tag) diff --git a/security/selinux/ss/avtab.h b/security/selinux/ss/avtab.h index 4c4445ca9118..f2eeb36265d1 100644 --- a/security/selinux/ss/avtab.h +++ b/security/selinux/ss/avtab.h @@ -89,7 +89,7 @@ struct avtab { void avtab_init(struct avtab *h); int avtab_alloc(struct avtab *, u32); -int avtab_duplicate(struct avtab *new, struct avtab *orig); +int avtab_alloc_dup(struct avtab *new, const struct avtab *orig); struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *k); void avtab_destroy(struct avtab *h); void avtab_hash_eval(struct avtab *h, char *tag); diff --git a/security/selinux/ss/conditional.c b/security/selinux/ss/conditional.c index 0b32f3ab025e..1ef74c085f2b 100644 --- a/security/selinux/ss/conditional.c +++ b/security/selinux/ss/conditional.c @@ -605,7 +605,6 @@ static int cond_dup_av_list(struct cond_av_list *new, struct cond_av_list *orig, struct avtab *avtab) { - struct avtab_node *avnode; u32 i; memset(new, 0, sizeof(*new)); @@ -615,10 +614,11 @@ static int cond_dup_av_list(struct cond_av_list *new, return -ENOMEM; for (i = 0; i < orig->len; i++) { - avnode = avtab_search_node(avtab, &orig->nodes[i]->key); - if (WARN_ON(!avnode)) - return -EINVAL; - new->nodes[i] = avnode; + new->nodes[i] = avtab_insert_nonunique(avtab, + &orig->nodes[i]->key, + &orig->nodes[i]->datum); + if (!new->nodes[i]) + return -ENOMEM; new->len++; } @@ -630,7 +630,7 @@ static int duplicate_policydb_cond_list(struct policydb *newp, { int rc, i, j; - rc = avtab_duplicate(&newp->te_cond_avtab, &origp->te_cond_avtab); + rc = avtab_alloc_dup(&newp->te_cond_avtab, &origp->te_cond_avtab); if (rc) return rc; From patchwork Tue Mar 30 13:16:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 12172555 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34864C433E1 for ; Tue, 30 Mar 2021 13:17:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6E64600CD for ; Tue, 30 Mar 2021 13:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231794AbhC3NRB (ORCPT ); Tue, 30 Mar 2021 09:17:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:53003 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232122AbhC3NQz (ORCPT ); Tue, 30 Mar 2021 09:16:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617110215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9pwB6l9VhZhkITrbtGxLTExvtQ9kVVt5unFgEjPuUiA=; b=cwWnZyU8O5BjMwy8fZbGnututSJAU+Guw5/7XJ2WcSaEPSxDDNpAFRM6QESBlegntTsipM MthkrGVztWhdF5NxXbtbdXNmFfhV44rP6Z1otjTby/+aOcuK8A6wrZd+7t+IoAQn16/8Cf f/2YwrvBji6wq4m7opVVhNrKYY6DYwc= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-340-k1rk3yenOYG0ZyWE1rPFLg-1; Tue, 30 Mar 2021 09:16:52 -0400 X-MC-Unique: k1rk3yenOYG0ZyWE1rPFLg-1 Received: by mail-ed1-f69.google.com with SMTP id y10so10156830edr.20 for ; Tue, 30 Mar 2021 06:16:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9pwB6l9VhZhkITrbtGxLTExvtQ9kVVt5unFgEjPuUiA=; b=lsExHxMEBPuWEoK0tEGEYuyISqbUZLlQj5DYzsai4Vl7CiXqZxy/VLAmppWdjIWVoj 1BajtkfmkDWdCDMIkDEvyTliOL6owsisrhrgyY0f/jLC8DS4T7/lacet0QCh9z/1sh4J 18M/zy3Nx9zvuIifVOW4T+VnvWzFDfeFL5Bu3Ks9U51o4qygjjR4WdaoEQKKNMw2OgPv 0HDo7xy/tb8nIO4VQpZUbAsB+ylaTR2l1tPQ95jIwC5JganjRS7APfs0r3IVXOILxd9t +SoiiHjNtKLxNWVCJ2uzleXRjVatnQaku3DBzC5WjW5OLnfHWGO2IjcELt19f2689BIG rUlw== X-Gm-Message-State: AOAM532lFVXqroMFHHGrnBPMtq5E/o2OEjwpi08gqu4swpUO3ZMN9cCb CCiy+HNYvsbuDHRNyGILYIFsU7MD/C+4E0GNxjL1dhnWQph8r68iBkzwFxvV0lTjB7OuSkDvPXl VyqSK6tBHCL6tJrqxAnXdUmLsvYSeQ1MQFHGPOID0HBbpRBSnJN0yIv4jA7t6niOmwPRdDA== X-Received: by 2002:a05:6402:888:: with SMTP id e8mr33072129edy.51.1617110211538; Tue, 30 Mar 2021 06:16:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEwPhv2Xgu6VCvPTLLDcsMfqpfizij2aOCOupirctgQRagwpkTf/+rppBvlK0zJKG0R0CZew== X-Received: by 2002:a05:6402:888:: with SMTP id e8mr33072104edy.51.1617110211346; Tue, 30 Mar 2021 06:16:51 -0700 (PDT) Received: from localhost.localdomain ([2a02:8308:b105:dd00:277b:6436:24db:9466]) by smtp.gmail.com with ESMTPSA id u16sm11178245edq.4.2021.03.30.06.16.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 06:16:50 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH 2/3] selinux: simplify duplicate_policydb_cond_list() by using kmemdup() Date: Tue, 30 Mar 2021 15:16:45 +0200 Message-Id: <20210330131646.1401838-3-omosnace@redhat.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210330131646.1401838-1-omosnace@redhat.com> References: <20210330131646.1401838-1-omosnace@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org We can do the allocation + copying of expr.nodes in one go using kmemdup(). Signed-off-by: Ondrej Mosnacek --- security/selinux/ss/conditional.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/security/selinux/ss/conditional.c b/security/selinux/ss/conditional.c index 1ef74c085f2b..f6dfa9c821d6 100644 --- a/security/selinux/ss/conditional.c +++ b/security/selinux/ss/conditional.c @@ -628,7 +628,8 @@ static int cond_dup_av_list(struct cond_av_list *new, static int duplicate_policydb_cond_list(struct policydb *newp, struct policydb *origp) { - int rc, i, j; + int rc; + u32 i; rc = avtab_alloc_dup(&newp->te_cond_avtab, &origp->te_cond_avtab); if (rc) @@ -648,12 +649,12 @@ static int duplicate_policydb_cond_list(struct policydb *newp, newp->cond_list_len++; newn->cur_state = orign->cur_state; - newn->expr.nodes = kcalloc(orign->expr.len, - sizeof(*newn->expr.nodes), GFP_KERNEL); + newn->expr.nodes = kmemdup(orign->expr.nodes, + orign->expr.len * sizeof(*orign->expr.nodes), + GFP_KERNEL); if (!newn->expr.nodes) goto error; - for (j = 0; j < orign->expr.len; j++) - newn->expr.nodes[j] = orign->expr.nodes[j]; + newn->expr.len = orign->expr.len; rc = cond_dup_av_list(&newn->true_list, &orign->true_list, From patchwork Tue Mar 30 13:16:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ondrej Mosnacek X-Patchwork-Id: 12172557 X-Patchwork-Delegate: paul@paul-moore.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02323C433E0 for ; Tue, 30 Mar 2021 13:17:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B4E12619BD for ; Tue, 30 Mar 2021 13:17:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232034AbhC3NRC (ORCPT ); Tue, 30 Mar 2021 09:17:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:59087 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232134AbhC3NQ4 (ORCPT ); Tue, 30 Mar 2021 09:16:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617110216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cgCviOM9iWG29JeybGODE4wzEs1gIDGTp3vx4pUISc8=; b=hBa+qySTgkVULqxZj7cW6XajXBbGwaga5dcj3L+umsBz13rY9B+pnUcv/mA5bhIqb/ix7u QdguDk0eOCCyoxIqDw7A1zglliUMVsr3kptgCyCtsvNYVQM9RC5WrYR6+ErO0aX6QWCUyr zGL2/12sOMKHNwXn9khx3auXXSvmusc= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-512-c9g7y1VjPpqzCLDkSeEsOQ-1; Tue, 30 Mar 2021 09:16:54 -0400 X-MC-Unique: c9g7y1VjPpqzCLDkSeEsOQ-1 Received: by mail-ed1-f70.google.com with SMTP id i12so6806053edu.23 for ; Tue, 30 Mar 2021 06:16:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cgCviOM9iWG29JeybGODE4wzEs1gIDGTp3vx4pUISc8=; b=ZtA5uhNN14p5oJP45LKUzutvOEvGG1F47qVA3xbmTpByAO/H2Q6jGCPLeo3fSCXD0V YLo4jB91JeKAeTW7xZE/x2XCEnq5tcxmZ7kG5NqTE7Wpkxpk//9vHKMKed/LQhhzT2s2 Yr0B1JWBeZ1MWoGJL5lRsdTGA05BoJICLbWOUaXiD3MAaDu97OwBMng5Tr/6ejwS9B/L y14H/f1Yo58rgQajRDaksB00rsya1IrmQVxj+P6TTOpdVjVlCUbsdnt2lej5sqxu7bE0 KF9xyfRmYipDAizqcepqy4N/pvn+6NhJlGq69b2AL3SBJYp0k9WWGflaq2cSZtYB1vzF qwXg== X-Gm-Message-State: AOAM533ejBWeljRHqC3HFu25ks7buT5N/UpMOSDge8CYN8kd6EEh29X/ ASWoJ2HYvcGH5Yre1Ugw91OoubvTUXUXhZ08li20wtCElKeXIyACtcuZv0Bg1EIzGLZQxtuVHK/ YVIPKnhVOugM+wPAnJqk13QHgLbT44pqFS09Z4vgCW8bG7xFpsiJIEGVo1HhuTH+t5B7m2Q== X-Received: by 2002:a05:6402:30bb:: with SMTP id df27mr16228002edb.147.1617110212980; Tue, 30 Mar 2021 06:16:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzNL4+qLDVJhPotzB4P/xQmzVS9ATqc40ihX8I6PwTSOQZeyp7yhrRJ1HdAlDjglpVns5jkxA== X-Received: by 2002:a05:6402:30bb:: with SMTP id df27mr16227977edb.147.1617110212768; Tue, 30 Mar 2021 06:16:52 -0700 (PDT) Received: from localhost.localdomain ([2a02:8308:b105:dd00:277b:6436:24db:9466]) by smtp.gmail.com with ESMTPSA id u16sm11178245edq.4.2021.03.30.06.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 06:16:52 -0700 (PDT) From: Ondrej Mosnacek To: selinux@vger.kernel.org, Paul Moore Cc: Stephen Smalley Subject: [PATCH 3/3] selinux: constify some avtab function arguments Date: Tue, 30 Mar 2021 15:16:46 +0200 Message-Id: <20210330131646.1401838-4-omosnace@redhat.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210330131646.1401838-1-omosnace@redhat.com> References: <20210330131646.1401838-1-omosnace@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: selinux@vger.kernel.org This makes the code a bit easier to reason about. Signed-off-by: Ondrej Mosnacek --- security/selinux/ss/avtab.c | 28 +++++++++++++++------------- security/selinux/ss/avtab.h | 16 +++++++++------- security/selinux/ss/conditional.c | 3 ++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/security/selinux/ss/avtab.c b/security/selinux/ss/avtab.c index 11f8f524de98..f6f09bb8116a 100644 --- a/security/selinux/ss/avtab.c +++ b/security/selinux/ss/avtab.c @@ -29,7 +29,7 @@ static struct kmem_cache *avtab_xperms_cachep __ro_after_init; /* Based on MurmurHash3, written by Austin Appleby and placed in the * public domain. */ -static inline int avtab_hash(struct avtab_key *keyp, u32 mask) +static inline int avtab_hash(const struct avtab_key *keyp, u32 mask) { static const u32 c1 = 0xcc9e2d51; static const u32 c2 = 0x1b873593; @@ -68,7 +68,7 @@ static inline int avtab_hash(struct avtab_key *keyp, u32 mask) static struct avtab_node* avtab_insert_node(struct avtab *h, int hvalue, struct avtab_node *prev, struct avtab_node *cur, - struct avtab_key *key, struct avtab_datum *datum) + const struct avtab_key *key, const struct avtab_datum *datum) { struct avtab_node *newnode; struct avtab_extended_perms *xperms; @@ -103,7 +103,8 @@ avtab_insert_node(struct avtab *h, int hvalue, return newnode; } -static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum) +static int avtab_insert(struct avtab *h, const struct avtab_key *key, + const struct avtab_datum *datum) { int hvalue; struct avtab_node *prev, *cur, *newnode; @@ -147,8 +148,9 @@ static int avtab_insert(struct avtab *h, struct avtab_key *key, struct avtab_dat * key/specified mask into the table, as needed by the conditional avtab. * It also returns a pointer to the node inserted. */ -struct avtab_node * -avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datum *datum) +struct avtab_node *avtab_insert_nonunique(struct avtab *h, + const struct avtab_key *key, + const struct avtab_datum *datum) { int hvalue; struct avtab_node *prev, *cur; @@ -178,7 +180,7 @@ avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, struct avtab_datu return avtab_insert_node(h, hvalue, prev, cur, key, datum); } -struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key) +struct avtab_datum *avtab_search(struct avtab *h, const struct avtab_key *key) { int hvalue; struct avtab_node *cur; @@ -213,8 +215,8 @@ struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *key) /* This search function returns a node pointer, and can be used in * conjunction with avtab_search_next_node() */ -struct avtab_node* -avtab_search_node(struct avtab *h, struct avtab_key *key) +struct avtab_node *avtab_search_node(struct avtab *h, + const struct avtab_key *key) { int hvalue; struct avtab_node *cur; @@ -393,8 +395,8 @@ static uint16_t spec_order[] = { }; int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol, - int (*insertf)(struct avtab *a, struct avtab_key *k, - struct avtab_datum *d, void *p), + int (*insertf)(struct avtab *a, const struct avtab_key *k, + const struct avtab_datum *d, void *p), void *p) { __le16 buf16[4]; @@ -554,8 +556,8 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol, return insertf(a, &key, &datum, p); } -static int avtab_insertf(struct avtab *a, struct avtab_key *k, - struct avtab_datum *d, void *p) +static int avtab_insertf(struct avtab *a, const struct avtab_key *k, + const struct avtab_datum *d, void *p) { return avtab_insert(a, k, d); } @@ -604,7 +606,7 @@ bad: goto out; } -int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp) +int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp) { __le16 buf16[4]; __le32 buf32[ARRAY_SIZE(cur->datum.u.xperms->perms.p)]; diff --git a/security/selinux/ss/avtab.h b/security/selinux/ss/avtab.h index f2eeb36265d1..d3ebea8d146f 100644 --- a/security/selinux/ss/avtab.h +++ b/security/selinux/ss/avtab.h @@ -90,24 +90,26 @@ struct avtab { void avtab_init(struct avtab *h); int avtab_alloc(struct avtab *, u32); int avtab_alloc_dup(struct avtab *new, const struct avtab *orig); -struct avtab_datum *avtab_search(struct avtab *h, struct avtab_key *k); +struct avtab_datum *avtab_search(struct avtab *h, const struct avtab_key *k); void avtab_destroy(struct avtab *h); void avtab_hash_eval(struct avtab *h, char *tag); struct policydb; int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol, - int (*insert)(struct avtab *a, struct avtab_key *k, - struct avtab_datum *d, void *p), + int (*insert)(struct avtab *a, const struct avtab_key *k, + const struct avtab_datum *d, void *p), void *p); int avtab_read(struct avtab *a, void *fp, struct policydb *pol); -int avtab_write_item(struct policydb *p, struct avtab_node *cur, void *fp); +int avtab_write_item(struct policydb *p, const struct avtab_node *cur, void *fp); int avtab_write(struct policydb *p, struct avtab *a, void *fp); -struct avtab_node *avtab_insert_nonunique(struct avtab *h, struct avtab_key *key, - struct avtab_datum *datum); +struct avtab_node *avtab_insert_nonunique(struct avtab *h, + const struct avtab_key *key, + const struct avtab_datum *datum); -struct avtab_node *avtab_search_node(struct avtab *h, struct avtab_key *key); +struct avtab_node *avtab_search_node(struct avtab *h, + const struct avtab_key *key); struct avtab_node *avtab_search_node_next(struct avtab_node *node, int specified); diff --git a/security/selinux/ss/conditional.c b/security/selinux/ss/conditional.c index f6dfa9c821d6..2ec6e5cd25d9 100644 --- a/security/selinux/ss/conditional.c +++ b/security/selinux/ss/conditional.c @@ -254,7 +254,8 @@ struct cond_insertf_data { struct cond_av_list *other; }; -static int cond_insertf(struct avtab *a, struct avtab_key *k, struct avtab_datum *d, void *ptr) +static int cond_insertf(struct avtab *a, const struct avtab_key *k, + const struct avtab_datum *d, void *ptr) { struct cond_insertf_data *data = ptr; struct policydb *p = data->p;