From patchwork Wed Nov 20 11:59:48 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: 13881092 Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD75A1A38D7 for ; Wed, 20 Nov 2024 12:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; cv=none; b=qlw8/d54zuFcji6gPNXL4xgtZE0uCguO0thVIfaafHetVe7fAtxAmx990XtboGiqEnU0Z0qrm4SH0NH9MDvQ/+5Bz6P5QAIcw9++y2SVZelZdjwQJKFM4TMMa5LcvT7MH73J9vIpj/NInjg3R8lewRuaAwqi+GtRd2UyT45SETQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; c=relaxed/simple; bh=x4VwERCSGgTpesze+z2jQypdpReyQfia6STeVdXwDBM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=SdwCadn0odLXhST5EKKTkYZQ3p3+cwlWyt99Y1fPZKyB35Q4N9cxPzuu1NW7Fq8Eq1Nbi9tkYkivCHb6t+xKO599WnBCOEAJHo91ZShtMq2FMzr4PTGarDP3ntvPtFLGzgm/auUf4dEOIGnKwWSBZkFB95jT3uvZkCKIotsD8M0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=Fo1ULaPe; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="Fo1ULaPe" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1732103997; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ef9GiNZ3kX3/0t0gBTeX3bOyu9WehiuBcfDripo7MBs=; b=Fo1ULaPeFmzQcE1k+dJP+72mO4k2A6baKiQSDJQJ03TKAWSTQW18xtTleHMaZmU8eNglj6 iOn/aYoOZJktHUf0G/NIaDXYlgO6pU1dYSTynb+c+HxJL/DtW0CDxssJVtDJvhXe67c2S2 GyVd0GXpxfmG4H2JFpCtG58AhAhHFuj+B+3ChPmalG4cAwb5ds0/YTQYt+tT3NTOBPoe/4 Kt7XZwepTzOnydgeX9gc7dA0VfCiVsCawH+uo5RMZm8Wxywv1uWF8FcGxVGJFJnaw70zkW RTgTWwVR4pDSgmCJ/bBekiZVsBj+skv8npTtkTAQ9sY95yYAMUxUgLTTY6QNJQ== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH v2 1/3] libselinux: make use of calloc(3) Date: Wed, 20 Nov 2024 12:59:48 +0100 Message-ID: <20241120115951.42445-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche Use calloc(3) instead of calling malloc(3) plus a call to memset(3) or manual zero'ing. Signed-off-by: Christian Göttsche --- libselinux/src/fgetfilecon.c | 3 +-- libselinux/src/get_context_list.c | 3 +-- libselinux/src/get_initial_context.c | 3 +-- libselinux/src/getfilecon.c | 3 +-- libselinux/src/getpeercon.c | 3 +-- libselinux/src/label_media.c | 6 ++---- libselinux/src/label_x.c | 6 ++---- libselinux/src/lgetfilecon.c | 3 +-- libselinux/src/matchpathcon.c | 3 +-- libselinux/src/procattr.c | 3 +-- libselinux/src/setrans_client.c | 5 ++--- 11 files changed, 14 insertions(+), 27 deletions(-) diff --git a/libselinux/src/fgetfilecon.c b/libselinux/src/fgetfilecon.c index d7051171..782861ed 100644 --- a/libselinux/src/fgetfilecon.c +++ b/libselinux/src/fgetfilecon.c @@ -39,10 +39,9 @@ int fgetfilecon_raw(int fd, char ** context) ssize_t ret; size = INITCONTEXTLEN + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) return -1; - memset(buf, 0, size); ret = fgetxattr_wrapper(fd, XATTR_NAME_SELINUX, buf, size - 1); if (ret < 0 && errno == ERANGE) { diff --git a/libselinux/src/get_context_list.c b/libselinux/src/get_context_list.c index 222b54c1..8d5ee6fb 100644 --- a/libselinux/src/get_context_list.c +++ b/libselinux/src/get_context_list.c @@ -481,12 +481,11 @@ int get_ordered_context_list(const char *user, the "failsafe" context to at least permit root login for emergency recovery if possible. */ freeconary(reachable); - reachable = malloc(2 * sizeof(char *)); + reachable = calloc(2, sizeof(char *)); if (!reachable) { rc = -1; goto out; } - reachable[0] = reachable[1] = 0; rc = get_failsafe_context(user, &reachable[0]); if (rc < 0) { freeconary(reachable); diff --git a/libselinux/src/get_initial_context.c b/libselinux/src/get_initial_context.c index 0f25ba3f..fb774c82 100644 --- a/libselinux/src/get_initial_context.c +++ b/libselinux/src/get_initial_context.c @@ -39,12 +39,11 @@ int security_get_initial_context_raw(const char * name, char ** con) return -1; size = selinux_page_size; - buf = malloc(size); + buf = calloc(1, size); if (!buf) { ret = -1; goto out; } - memset(buf, 0, size); ret = read(fd, buf, size - 1); if (ret < 0) goto out2; diff --git a/libselinux/src/getfilecon.c b/libselinux/src/getfilecon.c index 4bee3137..31c9f0de 100644 --- a/libselinux/src/getfilecon.c +++ b/libselinux/src/getfilecon.c @@ -14,10 +14,9 @@ int getfilecon_raw(const char *path, char ** context) ssize_t ret; size = INITCONTEXTLEN + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) return -1; - memset(buf, 0, size); ret = getxattr(path, XATTR_NAME_SELINUX, buf, size - 1); if (ret < 0 && errno == ERANGE) { diff --git a/libselinux/src/getpeercon.c b/libselinux/src/getpeercon.c index a9dca73e..c7abd886 100644 --- a/libselinux/src/getpeercon.c +++ b/libselinux/src/getpeercon.c @@ -18,10 +18,9 @@ int getpeercon_raw(int fd, char ** context) ssize_t ret; size = INITCONTEXTLEN + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) return -1; - memset(buf, 0, size); ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size); if (ret < 0 && errno == ERANGE) { diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c index 0510b5b1..be3df388 100644 --- a/libselinux/src/label_media.c +++ b/libselinux/src/label_media.c @@ -134,10 +134,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, status = 0; goto finish; } - data->spec_arr = malloc(sizeof(spec_t)*data->nspec); + data->spec_arr = calloc(data->nspec, sizeof(spec_t)); if (data->spec_arr == NULL) goto finish; - memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec); maxnspec = data->nspec; status = fseek(fp, 0L, SEEK_SET); @@ -230,10 +229,9 @@ int selabel_media_init(struct selabel_handle *rec, { struct saved_data *data; - data = (struct saved_data *)malloc(sizeof(*data)); + data = (struct saved_data *)calloc(1, sizeof(*data)); if (!data) return -1; - memset(data, 0, sizeof(*data)); rec->data = data; rec->func_close = &close; diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c index 1a5b9268..5b0e4063 100644 --- a/libselinux/src/label_x.c +++ b/libselinux/src/label_x.c @@ -161,10 +161,9 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts, status = 0; goto finish; } - data->spec_arr = malloc(sizeof(spec_t)*data->nspec); + data->spec_arr = calloc(data->nspec, sizeof(spec_t)); if (data->spec_arr == NULL) goto finish; - memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec); maxnspec = data->nspec; status = fseek(fp, 0L, SEEK_SET); @@ -255,10 +254,9 @@ int selabel_x_init(struct selabel_handle *rec, const struct selinux_opt *opts, { struct saved_data *data; - data = (struct saved_data *)malloc(sizeof(*data)); + data = (struct saved_data *)calloc(1, sizeof(*data)); if (!data) return -1; - memset(data, 0, sizeof(*data)); rec->data = data; rec->func_close = &close; diff --git a/libselinux/src/lgetfilecon.c b/libselinux/src/lgetfilecon.c index d1fb821b..f0a3aa1a 100644 --- a/libselinux/src/lgetfilecon.c +++ b/libselinux/src/lgetfilecon.c @@ -14,10 +14,9 @@ int lgetfilecon_raw(const char *path, char ** context) ssize_t ret; size = INITCONTEXTLEN + 1; - buf = malloc(size); + buf = calloc(1, size); if (!buf) return -1; - memset(buf, 0, size); ret = lgetxattr(path, XATTR_NAME_SELINUX, buf, size - 1); if (ret < 0 && errno == ERANGE) { diff --git a/libselinux/src/matchpathcon.c b/libselinux/src/matchpathcon.c index 967520e4..dbc11ee1 100644 --- a/libselinux/src/matchpathcon.c +++ b/libselinux/src/matchpathcon.c @@ -202,10 +202,9 @@ int matchpathcon_filespec_add(ino_t ino, int specind, const char *file) struct stat sb; if (!fl_head) { - fl_head = malloc(sizeof(file_spec_t) * HASH_BUCKETS); + fl_head = calloc(HASH_BUCKETS, sizeof(file_spec_t)); if (!fl_head) goto oom; - memset(fl_head, 0, sizeof(file_spec_t) * HASH_BUCKETS); } h = (ino + (ino >> HASH_BITS)) & HASH_MASK; diff --git a/libselinux/src/procattr.c b/libselinux/src/procattr.c index b7a93a2b..ddcc7f8d 100644 --- a/libselinux/src/procattr.c +++ b/libselinux/src/procattr.c @@ -139,12 +139,11 @@ static int getprocattrcon_raw(char **context, pid_t pid, const char *attr, return -1; size = selinux_page_size; - buf = malloc(size); + buf = calloc(1, size); if (!buf) { ret = -1; goto out; } - memset(buf, 0, size); do { ret = read(fd, buf, size - 1); diff --git a/libselinux/src/setrans_client.c b/libselinux/src/setrans_client.c index d7dbc0ca..45cbe5c1 100644 --- a/libselinux/src/setrans_client.c +++ b/libselinux/src/setrans_client.c @@ -173,11 +173,10 @@ receive_response(int fd, uint32_t function, char **outdata, int32_t * ret_val) return -1; } - data = malloc(data_size); + /* coveriety doesn't realize that data will be initialized in readv */ + data = calloc(1, data_size); if (!data) return -1; - /* coveriety doesn't realize that data will be initialized in readv */ - memset(data, 0, data_size); resp_data.iov_base = data; resp_data.iov_len = data_size; From patchwork Wed Nov 20 11:59:49 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: 13881091 Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD6401A304A for ; Wed, 20 Nov 2024 12:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; cv=none; b=Jadx3FP3eKkyf7M4VaMtC+4ssEp/ts/PbK6MQ3fS5uBqnQHciOJ9DlgJXE00xuaE2ZBER9irTDpCRLIubAqFpvEgPjKsy+RgxJAVpvAiqQEKYUzNz5wbdBpTzBIzXKFOeyXoV6IfcxMorfnUVtca5TjWeRan7doQxYBXeHAeUW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; c=relaxed/simple; bh=EfQ7pK6H9YpoRBwPI8SnLEvOBMtI8vhlF2AE66fqNAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=V9qLx4X8ETTRVz54TYuRMBQe9nbHg65VVasFcNCVH6a/kiccm7qZwZjUDsdn8O8fGYCA2fkBE/43iKgp9JqlAt20ALqRiLiHTmsRWkjff023eatxk3SyLvYu8fHfZ2LL98tnAWQo9X1enKmrJgRSTx2lI6LSRQ+7T0eVvwfgZoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=Dm/FUqiX; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="Dm/FUqiX" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1732103997; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t7YQJpHwARV1qjeXpQlQtFPQFwcSXUfx7symzkpKA34=; b=Dm/FUqiXtaA3J89gpVjCQdGjcgJpDhXnEEFpS+0MKyK0i3vqAt7BME+T6j3tIwKmwNVveH ezy6q/x294WAruLrnquIiVvGOVpzxVdoqoJH9hPHAcns54pZUCH1fp6Xm1h//9YrE+Ulha zg+hekkqECeCRDkicjpE4rqdEvY8ww5PFp/6QaJzW0ZVi9cI6mMwJD0LHNMxRos57OhdH+ iczQqEIbV+6O98WEKMgQ0VRYjciXMAh7yEGGfSlSdBDQ8b0ytXOl1qTy6gU4/2IsJjMNe6 gl46GocOcjO0yccnRSgncL3BQYxa6iWBjAe6PmVBu9LOVRnDAt3kl0hs4XRQKw== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH v2 2/3] libselinux: avoid dynamic allocation in openattr() Date: Wed, 20 Nov 2024 12:59:49 +0100 Message-ID: <20241120115951.42445-2-cgoettsche@seltendoof.de> In-Reply-To: <20241120115951.42445-1-cgoettsche@seltendoof.de> References: <20241120115951.42445-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche openattr() supplies the simplementation for the getcon(3) interface family. Use a short local buffer instead of descend into memory allocation. Signed-off-by: Christian Göttsche --- v2: - minimize buffer from 56 to 44 characters and assert pid_t is not wider than 32bit Signed-off-by: Christian Göttsche --- libselinux/src/procattr.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libselinux/src/procattr.c b/libselinux/src/procattr.c index ddcc7f8d..21c810d2 100644 --- a/libselinux/src/procattr.c +++ b/libselinux/src/procattr.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -86,32 +87,34 @@ static void init_procattr(void) static int openattr(pid_t pid, const char *attr, int flags) { int fd, rc; - char *path; + char path[44]; /* must hold "/proc/self/task/%d/attr/sockcreate" */ pid_t tid; + static_assert(sizeof(pid_t) <= 4, "content written to path might get truncated"); + if (pid > 0) { - rc = asprintf(&path, "/proc/%d/attr/%s", pid, attr); + rc = snprintf(path, sizeof(path), "/proc/%d/attr/%s", pid, attr); } else if (pid == 0) { - rc = asprintf(&path, "/proc/thread-self/attr/%s", attr); - if (rc < 0) + rc = snprintf(path, sizeof(path), "/proc/thread-self/attr/%s", attr); + if (rc < 0 || (size_t)rc >= sizeof(path)) { + errno = EOVERFLOW; return -1; + } fd = open(path, flags | O_CLOEXEC); if (fd >= 0 || errno != ENOENT) - goto out; - free(path); + return fd; tid = selinux_gettid(); - rc = asprintf(&path, "/proc/self/task/%d/attr/%s", tid, attr); + rc = snprintf(path, sizeof(path), "/proc/self/task/%d/attr/%s", tid, attr); } else { errno = EINVAL; return -1; } - if (rc < 0) + if (rc < 0 || (size_t)rc >= sizeof(path)) { + errno = EOVERFLOW; return -1; + } - fd = open(path, flags | O_CLOEXEC); -out: - free(path); - return fd; + return open(path, flags | O_CLOEXEC); } static int getprocattrcon_raw(char **context, pid_t pid, const char *attr, From patchwork Wed Nov 20 11:59:50 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: 13881093 Received: from server02.seltendoof.de (server02.seltendoof.de [168.119.48.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD6CC1A3056 for ; Wed, 20 Nov 2024 12:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.48.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; cv=none; b=hOqos6FTKLu4awRndtXNdiT6+lGsMWtrA9OvEcmWWSjXbZYmDsaToOK32VMBGFtv9w7crU8c0gYGnljwjpjVR/BX/Q03TOH5DutFkKPG2Ln4i1MmFYY0fh1m1rywrlfVfgDMBqE8UtP3/UgJeImufhek6+LapMBFumZLogiIQGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732104009; c=relaxed/simple; bh=xnD31VWmzgT/zx+OloSCWZuZ9QXBXBf5xpSHz6ieICw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nNdepmac/R9v7jh8rHzkq2WdL4PHopogP+0d+WbeLbSdq1gXxryprR1O4S4hfUIVHseGsii9Rhbs2Go3FxKuLDVCQdGjkZ/8E5WKG8wNA+0aBI7cXaP4cDhDbME4172uoyKE4BdiHL15QoIPh7MCiTXQrSeTEDczdwsxAi54zoU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de; spf=pass smtp.mailfrom=seltendoof.de; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b=Gke0Yll6; arc=none smtp.client-ip=168.119.48.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=seltendoof.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=seltendoof.de header.i=@seltendoof.de header.b="Gke0Yll6" From: =?utf-8?q?Christian_G=C3=B6ttsche?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seltendoof.de; s=2023072701; t=1732103998; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W7z1uVjloLm6oWfzAA7CtwZwufEXxECXJ2j3hatsY8Q=; b=Gke0Yll6lh73N69thjtQ3ga1JMVn7UWF74wgg0mOG/wzna/wpUWpi5DpypnlaGuHgH1aRz ZMJl2kgcS540odh878eoL0Nm01eVTH4VnFQLx/pktLrNIPMmAaXZ4dcM8wLOTpRkFIM11/ 6EQ85NNxVhfw4M4IAL/Kcc+NJeL5q+/YbftA8YiJ9YQYUxoT0xBIKeZKGce0dM9AGOmej/ t3ULLJCzCFHgWsyI/SzUEDkG6H6qjTfQs0oOOjNGsUXaK0CND0vBJYZrDH4qahp6/TDaQ+ V7B4sJUl92E1GQJnRChhJQxoepWXq0Guts3ypDpypMNg2kps01lsPaf6cu9MWA== To: selinux@vger.kernel.org Cc: =?utf-8?q?Christian_G=C3=B6ttsche?= Subject: [PATCH v2 3/3] libselinux: move functions out of header file Date: Wed, 20 Nov 2024 12:59:50 +0100 Message-ID: <20241120115951.42445-3-cgoettsche@seltendoof.de> In-Reply-To: <20241120115951.42445-1-cgoettsche@seltendoof.de> References: <20241120115951.42445-1-cgoettsche@seltendoof.de> Reply-To: cgzones@googlemail.com Precedence: bulk X-Mailing-List: selinux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Christian Göttsche When building libselinux from its own directory GCC complains about the two functions free_spec_node() and sort_spec_node(), which are not tiny and also recursive. In file included from label_file.c:27: In function ‘load_mmap’, inlined from ‘process_file’ at label_file.c:1106:9: label_file.h:816:20: error: inlining failed in call to ‘free_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline] 816 | static inline void free_spec_node(struct spec_node *node) | ^~~~~~~~~~~~~~ label_file.c:899:17: note: called from here 899 | free_spec_node(data->root); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ label_file.h:816:20: error: inlining failed in call to ‘free_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline] 816 | static inline void free_spec_node(struct spec_node *node) | ^~~~~~~~~~~~~~ label_file.c:908:17: note: called from here 908 | free_spec_node(root); | ^~~~~~~~~~~~~~~~~~~~ In function ‘sort_specs’, inlined from ‘init’ at label_file.c:1350:3: label_file.h:404:20: error: inlining failed in call to ‘sort_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline] 404 | static inline void sort_spec_node(struct spec_node *node, struct spec_node *parent) | ^~~~~~~~~~~~~~ label_file.h:433:9: note: called from here 433 | sort_spec_node(data->root, NULL); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In function ‘sort_specs’, inlined from ‘init’ at label_file.c:1370:3: label_file.h:404:20: error: inlining failed in call to ‘sort_spec_node’: --param max-inline-insns-single limit reached [-Werror=inline] 404 | static inline void sort_spec_node(struct spec_node *node, struct spec_node *parent) | ^~~~~~~~~~~~~~ label_file.h:433:9: note: called from here 433 | sort_spec_node(data->root, NULL); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fixes: 92306daf ("libselinux: rework selabel_file(5) database") Signed-off-by: Christian Göttsche --- v2: - add patch --- libselinux/src/label_file.c | 68 +++++++++++++++++++++++++++++++++++ libselinux/src/label_file.h | 71 ++----------------------------------- 2 files changed, 71 insertions(+), 68 deletions(-) diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 189a5ed2..40bcb9ee 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -34,6 +34,74 @@ #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ +void free_spec_node(struct spec_node *node) +{ + for (uint32_t i = 0; i < node->literal_specs_num; i++) { + struct literal_spec *lspec = &node->literal_specs[i]; + + free(lspec->lr.ctx_raw); + free(lspec->lr.ctx_trans); + __pthread_mutex_destroy(&lspec->lr.lock); + + if (lspec->from_mmap) + continue; + + free(lspec->literal_match); + free(lspec->regex_str); + } + free(node->literal_specs); + + for (uint32_t i = 0; i < node->regex_specs_num; i++) { + struct regex_spec *rspec = &node->regex_specs[i]; + + free(rspec->lr.ctx_raw); + free(rspec->lr.ctx_trans); + __pthread_mutex_destroy(&rspec->lr.lock); + regex_data_free(rspec->regex); + __pthread_mutex_destroy(&rspec->regex_lock); + + if (rspec->from_mmap) + continue; + + free(rspec->regex_str); + } + free(node->regex_specs); + + for (uint32_t i = 0; i < node->children_num; i++) + free_spec_node(&node->children[i]); + free(node->children); + + if (!node->from_mmap) + free(node->stem); +} + +void sort_spec_node(struct spec_node *node, struct spec_node *parent) +{ + /* A node should not be its own parent */ + assert(node != parent); + /* Only root node has NULL stem */ + assert((!parent && !node->stem) || (parent && node->stem && node->stem[0] != '\0')); + /* A non-root node should not be empty */ + assert(!parent || (node->literal_specs_num || node->regex_specs_num || node->children_num)); + + + node->parent = parent; + + /* Sort for comparison support and binary search lookup */ + + if (node->literal_specs_num > 1) + qsort(node->literal_specs, node->literal_specs_num, sizeof(struct literal_spec), compare_literal_spec); + + if (node->regex_specs_num > 1) + qsort(node->regex_specs, node->regex_specs_num, sizeof(struct regex_spec), compare_regex_spec); + + if (node->children_num > 1) + qsort(node->children, node->children_num, sizeof(struct spec_node), compare_spec_node); + + for (uint32_t i = 0; i < node->children_num; i++) + sort_spec_node(&node->children[i], node); +} + /* * Warn about duplicate specifications. */ diff --git a/libselinux/src/label_file.h b/libselinux/src/label_file.h index de8190f9..b59db003 100644 --- a/libselinux/src/label_file.h +++ b/libselinux/src/label_file.h @@ -164,6 +164,9 @@ struct saved_data { struct selabel_sub *subs; }; +void free_spec_node(struct spec_node *node); +void sort_spec_node(struct spec_node *node, struct spec_node *parent); + static inline mode_t string_to_file_kind(const char *mode) { if (mode[0] != '-' || mode[1] == '\0' || mode[2] != '\0') @@ -401,33 +404,6 @@ static inline int compare_spec_node(const void *p1, const void *p2) return rc; } -static inline void sort_spec_node(struct spec_node *node, struct spec_node *parent) -{ - /* A node should not be its own parent */ - assert(node != parent); - /* Only root node has NULL stem */ - assert((!parent && !node->stem) || (parent && node->stem && node->stem[0] != '\0')); - /* A non-root node should not be empty */ - assert(!parent || (node->literal_specs_num || node->regex_specs_num || node->children_num)); - - - node->parent = parent; - - /* Sort for comparison support and binary search lookup */ - - if (node->literal_specs_num > 1) - qsort(node->literal_specs, node->literal_specs_num, sizeof(struct literal_spec), compare_literal_spec); - - if (node->regex_specs_num > 1) - qsort(node->regex_specs, node->regex_specs_num, sizeof(struct regex_spec), compare_regex_spec); - - if (node->children_num > 1) - qsort(node->children, node->children_num, sizeof(struct spec_node), compare_spec_node); - - for (uint32_t i = 0; i < node->children_num; i++) - sort_spec_node(&node->children[i], node); -} - static inline void sort_specs(struct saved_data *data) { sort_spec_node(data->root, NULL); @@ -813,47 +789,6 @@ static int insert_spec(const struct selabel_handle *rec, struct saved_data *data #undef GROW_ARRAY -static inline void free_spec_node(struct spec_node *node) -{ - for (uint32_t i = 0; i < node->literal_specs_num; i++) { - struct literal_spec *lspec = &node->literal_specs[i]; - - free(lspec->lr.ctx_raw); - free(lspec->lr.ctx_trans); - __pthread_mutex_destroy(&lspec->lr.lock); - - if (lspec->from_mmap) - continue; - - free(lspec->literal_match); - free(lspec->regex_str); - } - free(node->literal_specs); - - for (uint32_t i = 0; i < node->regex_specs_num; i++) { - struct regex_spec *rspec = &node->regex_specs[i]; - - free(rspec->lr.ctx_raw); - free(rspec->lr.ctx_trans); - __pthread_mutex_destroy(&rspec->lr.lock); - regex_data_free(rspec->regex); - __pthread_mutex_destroy(&rspec->regex_lock); - - if (rspec->from_mmap) - continue; - - free(rspec->regex_str); - } - free(node->regex_specs); - - for (uint32_t i = 0; i < node->children_num; i++) - free_spec_node(&node->children[i]); - free(node->children); - - if (!node->from_mmap) - free(node->stem); -} - /* This will always check for buffer over-runs and either read the next entry * if buf != NULL or skip over the entry (as these areas are mapped in the * current buffer). */