From patchwork Fri Mar 15 18:14:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_G=C3=B6ttsche?= X-Patchwork-Id: 13593859 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EC474CE1B; Fri, 15 Mar 2024 18:14:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710526465; cv=none; b=P6K0+YUxo6q5xLBebD7MG4RRBUH7W/xGL0ZSwQcLuVjIa/YhkAUFtECaTl03NeRh/Rxqd9JDvn89fzjLjemE1wa3FQtzdq6KLmeOyX88bZLkWFBNBMO5LzjvO5yE+nD9emwbDrGY6dpRyi+aWDRiCHwVK4AXZSUxjBqHhLDvccE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710526465; c=relaxed/simple; bh=9MvyuDrhaotfv+B09py4H758keObr5ydb2VtJa2MuPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dOwd0DIYAfv3F3PysUY5eGpd6pFnw4/g+fPWvsq+7wdlIvaWiRgWlDuIzTPmDm+Y8kejiUUXSYhKKqjeYRJzM1/WcoAlvHpTBelV4K85Mnp8i9TU9T7nM3mB0Cc87Qf3X5H7PEpDD4h4vcjLT+H651nFIDYArDlUSnXov2n4Q4k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com; spf=pass smtp.mailfrom=googlemail.com; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b=FwNUO8kx; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b="FwNUO8kx" Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-5131a9b3d5bso2838740e87.0; Fri, 15 Mar 2024 11:14:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1710526459; x=1711131259; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rkDC2yEMoA8aKIWbb14yRjg2ggB/5aRYuGOEZHBpmZY=; b=FwNUO8kxELT0PVYOpStNAXEMmPU/k8dbMcyXoQgC91w2KQjmpXLwr6UdWnTN193HkJ UvhiswazO/1tcYzFnRty8AAyBBV8+XBHGjMB6Y1bCGO5gdbTJPkPyKkieCVmd4T6ms9K taE3fi/5q1jBmQJm2qqmutJ/mh9zDkFwZTuCx/WFyN4FT5tjJUZHA9u8vQr6xyPpk6RC vbPPncCAhDKFxswudmWkw9R2ZZbkxXZOlLXSoqRUHb8W2Ib8MuYwVvl6vOKPsJpeNbmd pkCYX2/p1Wi/cEJ3WJOGul8JOmVuhSBNdGsVB0CIUhvFHfS4iXg6wGCYL/6CPtNijXG9 d5XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710526459; x=1711131259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rkDC2yEMoA8aKIWbb14yRjg2ggB/5aRYuGOEZHBpmZY=; b=AqRFMSK5HGLqK/xR5HJtuHSUVlm6vYSJV6PfhGLaUr2eLa9Km8DGYkKof8WCS0Y/uB PhgLgc6BD5mNsRrgUppcUXvvoW5/EB99RrAd6rBByJr7rS2ydR9tBeKQDadZrdmWt2e7 Llrjkplt2Qq5lPVFX8gIRf1D0kpJwO/uem7UlHMymnI1anJ/7h8ygtfKZEDPc874JknS H4c68v2y4YV2AH07mjBK+nT0taL+Kjk0XLC13soqUfF9PrZOcZ7xbdZnMcAqpuNIjCI9 KykZcYZUdgpJc4/EtgZ8yUMggObRjC6zZy8Qct1/5iAvvv+gcf3ZbNBVyrULV/ie2l5J SlCg== X-Forwarded-Encrypted: i=1; AJvYcCXPYVP7WxLyJgsQb76L2QsgLonqLFFGP7fi5HeK2yrPXb0hpFW2PRctBKC/OAdI/zadKaGQDjUFQEkIHFUq0UynvYyQtfd+Gji0ysMq X-Gm-Message-State: AOJu0Yz9QiyHVxt5RJYOxkCWV2xziXK0FEqJP/rufuuV6hc17/vuHvVt 5PnWG9Zak5O/gxTVCO6Q65vcrRbV9BD8lapx+Ee3TQkdR+S3X47uLcPVoDWJ4iFVyA== X-Google-Smtp-Source: AGHT+IGIuF8DT4BuRTsNF75XWJY7LrK3fZ/F9lk3g/712vFhApFrvdmY7Sv/cGViWzJMkNiVQb4f8Q== X-Received: by 2002:ac2:5b03:0:b0:513:bd72:a677 with SMTP id v3-20020ac25b03000000b00513bd72a677mr3893522lfn.19.1710526459502; Fri, 15 Mar 2024 11:14:19 -0700 (PDT) Received: from ddev.DebianHome (dynamic-095-119-217-226.95.119.pool.telefonica.de. [95.119.217.226]) by smtp.gmail.com with ESMTPSA id u20-20020a1709060b1400b00a449076d0dbsm1915682ejg.53.2024.03.15.11.14.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 11:14:19 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Cc: Paul Moore , Stephen Smalley , Ondrej Mosnacek , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] selinux: dump statistics for more hash tables Date: Fri, 15 Mar 2024 19:14:05 +0100 Message-ID: <20240315181414.649045-2-cgzones@googlemail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240315181414.649045-1-cgzones@googlemail.com> References: <20240315181414.649045-1-cgzones@googlemail.com> Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Dump in the SELinux debug configuration the statistics for the conditional rules avtab, the role transition, and class and common permission hash tables. Signed-off-by: Christian Göttsche --- v2: print key in hash_eval() for class and common tables --- security/selinux/ss/conditional.c | 3 +++ security/selinux/ss/policydb.c | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/security/selinux/ss/conditional.c b/security/selinux/ss/conditional.c index f12476855b27..e868fc403d75 100644 --- a/security/selinux/ss/conditional.c +++ b/security/selinux/ss/conditional.c @@ -169,6 +169,9 @@ int cond_init_bool_indexes(struct policydb *p) p->p_bools.nprim, sizeof(*p->bool_val_to_struct), GFP_KERNEL); if (!p->bool_val_to_struct) return -ENOMEM; + + avtab_hash_eval(&p->te_cond_avtab, "conditional_rules"); + return 0; } diff --git a/security/selinux/ss/policydb.c b/security/selinux/ss/policydb.c index 3d22d5baa829..b0f688fe0737 100644 --- a/security/selinux/ss/policydb.c +++ b/security/selinux/ss/policydb.c @@ -672,14 +672,17 @@ static int (*const index_f[SYM_NUM])(void *key, void *datum, void *datap) = { /* clang-format on */ #ifdef CONFIG_SECURITY_SELINUX_DEBUG -static void hash_eval(struct hashtab *h, const char *hash_name) +static void hash_eval(struct hashtab *h, const char *hash_name, const char *hash_details) { struct hashtab_info info; hashtab_stat(h, &info); pr_debug( - "SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d, sum of chain length^2 %llu\n", - hash_name, h->nel, info.slots_used, h->size, info.max_chain_len, + "SELinux: %s%s%s: %d entries and %d/%d buckets used, longest chain length %d, sum of chain length^2 %llu\n", + hash_name, + hash_details ? "@" : "", + hash_details ?: "", + h->nel, info.slots_used, h->size, info.max_chain_len, info.chain2_len_sum); } @@ -688,11 +691,11 @@ static void symtab_hash_eval(struct symtab *s) int i; for (i = 0; i < SYM_NUM; i++) - hash_eval(&s[i].table, symtab_name[i]); + hash_eval(&s[i].table, symtab_name[i], NULL); } #else -static inline void hash_eval(struct hashtab *h, const char *hash_name) +static inline void hash_eval(struct hashtab *h, const char *hash_name, const char *hash_details) { } static inline void symtab_hash_eval(struct symtab *s) @@ -1178,6 +1181,8 @@ static int common_read(struct policydb *p, struct symtab *s, void *fp) goto bad; } + hash_eval(&comdatum->permissions.table, "common_permissions", key); + rc = symtab_insert(s, key, comdatum); if (rc) goto bad; @@ -1358,6 +1363,8 @@ static int class_read(struct policydb *p, struct symtab *s, void *fp) goto bad; } + hash_eval(&cladatum->permissions.table, "class_permissions", key); + rc = read_cons_helper(p, &cladatum->constraints, ncons, 0, fp); if (rc) goto bad; @@ -1898,7 +1905,7 @@ static int range_read(struct policydb *p, void *fp) rt = NULL; r = NULL; } - hash_eval(&p->range_tr, "rangetr"); + hash_eval(&p->range_tr, "rangetr", NULL); rc = 0; out: kfree(rt); @@ -2116,7 +2123,7 @@ static int filename_trans_read(struct policydb *p, void *fp) return rc; } } - hash_eval(&p->filename_trans, "filenametr"); + hash_eval(&p->filename_trans, "filenametr", NULL); return 0; } @@ -2649,6 +2656,8 @@ int policydb_read(struct policydb *p, void *fp) rtd = NULL; } + hash_eval(&p->role_tr, "roletr", NULL); + rc = next_entry(buf, fp, sizeof(u32)); if (rc) goto bad; From patchwork Fri Mar 15 18:14:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Christian_G=C3=B6ttsche?= X-Patchwork-Id: 13593858 X-Patchwork-Delegate: paul@paul-moore.com Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8136B4CB3D; Fri, 15 Mar 2024 18:14:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710526464; cv=none; b=WwI+8y9UniOVB1GCSH5bACex6T/00KxF8N0SUPe1jmfDXNj8wXuHYYk7FchtCRHvIoXpg0TC4A640lS+L7lWn0/oBke7NvCoW24HxLNkRvRxjZ15D+xK0gdl7ufMMrkpFsYmd0wcmQPY0hs8vNPjQ0+b0/DZfdY/d6cD/PtoCp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710526464; c=relaxed/simple; bh=oaDXv1OJE51+9/GVUC/OBvla4qNIWodt+ovWJPKnDyc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=c8T43Fi+78I4Jo3WT8/VYenzxLdBefUB1BtJ3z/ziXOu75piHQu3tacIhPkigyJ8YAgNOlX9mhgSvqEXlvETYrA0K/3MxizHFDZ7D0npUaaeEKNe45mcnBNLzySqHChOZ82/jqmJP1Jk1VlllV2aM9Uvf8oPDVYReqX/339rk4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com; spf=pass smtp.mailfrom=googlemail.com; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b=nicKShIm; arc=none smtp.client-ip=209.85.167.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.b="nicKShIm" Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-512bde3d197so2389190e87.0; Fri, 15 Mar 2024 11:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20230601; t=1710526458; x=1711131258; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GWhQJIrlAe3Zt6yVWAsx1dDzPBZeGyd2jOUBiZHxlQs=; b=nicKShImym04Tk5ubOrZ2vGfPRMS7qGX+Ruuos3YthVZgGawo1zeAGhx9mrDdAQgiE 0QZF7u2fOiJTdknfGYebcjqvhiUkWtU9qzZFX/SPmSyG9Tz3t6c1K4siSPjZaDbZUckw IK7eM0I++rcU3NGhD6mtzDUrsMZLpIJUEKm6+mbxfVQu29nrxG9xgji7KYsylgMukoJF esEVvVnf+TfJ3W2Z087ekY5mheiTcrRGnStwrM+tOeSInrvDJ1YwgOL4apIyhKEQpRW4 3ZVwIJavviZH5AS+XPpLUHCZGn/xcaU6C7OXfWaxmMGNJ9+yv8kOXan1+KaZFCgR6u2G foBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710526458; x=1711131258; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GWhQJIrlAe3Zt6yVWAsx1dDzPBZeGyd2jOUBiZHxlQs=; b=v1fWAQplWnm8QlB74nCJJefW7wvtgF2Tc2Na+t6SAPHKQhSF4DxOZJD9DFLTRdHzrh KUZ/WtODVYMOH1R6VGctswBtm+CNQ6TYwswuSL4+NJaFmExol/OLfR/MyuwMwYJ/8yDk oSKmJPmSoquPS+4f58QF8TG6Q6+p/qqa7UWyXqR2paXoR6X8pFKPz2k7An+WFSpq4y58 GRnSjkKaIkDsQJVHg04OVYu1PJin+PuYlnBT7x4qotBALQujRB9Uo1ldQ+j74v9g/QR+ ezzhsw/JFmBXgo7i3tBQ9yk+sw1zvXqR53CbnzatjpvdJtQfuL86nVllL0oZbSmlZmat UKXg== X-Forwarded-Encrypted: i=1; AJvYcCWtjoyE9VhJ8GXu/7WuM8SqTRiN9B3bvpRdi5PULyBwHMrZGo0wurwOCNji0eVdh4fIi8PNE+frvmcj1yEYzaUaFGIANeXm5jy0BULC X-Gm-Message-State: AOJu0YxY2h5SUEco5lywkY657KO8EVhhnnWW0KrvhljMjGg+rafMc6ai OvgZZGW5kV5vY9SAR9WJqONqCBCavN8cIRPNh44C13UXQdPy/DgcT0+98h+bLYjbIA== X-Google-Smtp-Source: AGHT+IHyYt6hSv3pFERpzItO8fZsQiuY5C6FoawT6+NaqEMW96OtRoSCcROYt+Xr/NzFYSYg16HdfQ== X-Received: by 2002:a19:8c01:0:b0:513:25c6:e98e with SMTP id o1-20020a198c01000000b0051325c6e98emr3720302lfd.1.1710526457609; Fri, 15 Mar 2024 11:14:17 -0700 (PDT) Received: from ddev.DebianHome (dynamic-095-119-217-226.95.119.pool.telefonica.de. [95.119.217.226]) by smtp.gmail.com with ESMTPSA id u20-20020a1709060b1400b00a449076d0dbsm1915682ejg.53.2024.03.15.11.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 11:14:17 -0700 (PDT) From: =?utf-8?q?Christian_G=C3=B6ttsche?= To: selinux@vger.kernel.org Cc: Paul Moore , Stephen Smalley , Ondrej Mosnacek , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] selinux: improve symtab string hashing Date: Fri, 15 Mar 2024 19:14:04 +0100 Message-ID: <20240315181414.649045-1-cgzones@googlemail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The number of buckets is calculated by performing a binary AND against the mask of the hash table, which is one less than its size (which is a power of two). This leads to all top bits being discarded, requiring for short or similar inputs a hash function with a good avalanche effect. Use djb2a: # current common prefixes: 7 entries and 5/8 buckets used, longest chain length 2, sum of chain length^2 11 classes: 134 entries and 100/256 buckets used, longest chain length 5, sum of chain length^2 234 roles: 15 entries and 6/16 buckets used, longest chain length 5, sum of chain length^2 57 types: 4448 entries and 3016/8192 buckets used, longest chain length 41, sum of chain length^2 14922 users: 7 entries and 3/8 buckets used, longest chain length 3, sum of chain length^2 17 bools: 306 entries and 221/512 buckets used, longest chain length 4, sum of chain length^2 524 levels: 1 entries and 1/1 buckets used, longest chain length 1, sum of chain length^2 1 categories: 1024 entries and 400/1024 buckets used, longest chain length 4, sum of chain length^2 2740 # patch common prefixes: 7 entries and 5/8 buckets used, longest chain length 2, sum of chain length^2 11 classes: 134 entries and 101/256 buckets used, longest chain length 3, sum of chain length^2 210 roles: 15 entries and 9/16 buckets used, longest chain length 3, sum of chain length^2 31 types: 4448 entries and 3459/8192 buckets used, longest chain length 5, sum of chain length^2 6778 users: 7 entries and 5/8 buckets used, longest chain length 3, sum of chain length^2 13 bools: 306 entries and 236/512 buckets used, longest chain length 5, sum of chain length^2 470 levels: 1 entries and 1/1 buckets used, longest chain length 1, sum of chain length^2 1 categories: 1024 entries and 518/1024 buckets used, longest chain length 7, sum of chain length^2 2992 Signed-off-by: Christian Göttsche --- v2: add licensing note --- security/selinux/ss/symtab.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/security/selinux/ss/symtab.c b/security/selinux/ss/symtab.c index c04f8d447873..832660fd84a9 100644 --- a/security/selinux/ss/symtab.c +++ b/security/selinux/ss/symtab.c @@ -12,17 +12,17 @@ static unsigned int symhash(const void *key) { - const char *p, *keyp; - unsigned int size; - unsigned int val; - - val = 0; - keyp = key; - size = strlen(keyp); - for (p = keyp; (p - keyp) < size; p++) - val = (val << 4 | (val >> (8 * sizeof(unsigned int) - 4))) ^ - (*p); - return val; + /* + * djb2a + * Public domain from cdb v0.75 + */ + unsigned int hash = 5381; + unsigned char c; + + while ((c = *(const unsigned char *)key++)) + hash = ((hash << 5) + hash) ^ c; + + return hash; } static int symcmp(const void *key1, const void *key2)