From patchwork Fri Mar 22 17:05:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13600322 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 E75BD5D75F; Fri, 22 Mar 2024 17:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127167; cv=none; b=CFab3naLbsnP4MwWmQIWHO3hfj9AnujXoYY9ST1xRqJ6Sd9D0KpRBnWCBUSZRPJdbYipwkRATA7U3IXPdKqHLAF8lCsH/fsBnOedUlB2n01CublcyAolYKUD/HDm8+QXmWF1Ylm3yQKmdgOEctNvrRPdZcfVwK+3gY6FBB4pRRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127167; c=relaxed/simple; bh=UNKfueCHNBcmXRHOtTAvJsS3aYDsMC1mej59C9wE9T8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J4OGxzDW1zSECvuTycu3qJxusy1kDzTFm1hn0pmXcy7XkSrRkeprBGJz4CIz0yGPNNYpE0+PypDBncj6caq4xBPsaDlPN3qJWdJ/NT3ZSdYl/Cl1g9rwNPMNgjbvPLEQzpJAWL2fZpcdCarHZfmrl/g9l6FnQ7D0GfssXOLanlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=iQny1SKi; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="iQny1SKi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1711127160; bh=UNKfueCHNBcmXRHOtTAvJsS3aYDsMC1mej59C9wE9T8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iQny1SKi+jtKTEA3LueQvoHYfTtxHWpeo92cQQ4OBMF1kOBAbiHy2zciRaL1HQwju BnKdgTXx2QNKvT7ZjfZtRSEGJJMOs3RQWIWwNN/XIlKdBBBg+/wFpEuVeaf1E+p24U O91a9sCeKmiuAZD//CWdJiaEr1JAuaWNYfVSPM2g= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Fri, 22 Mar 2024 18:05:56 +0100 Subject: [PATCH v2 1/3] sysctl: drop sysctl_is_perm_empty_ctl_table Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240322-sysctl-empty-dir-v2-1-e559cf8ec7c0@weissschuh.net> References: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> In-Reply-To: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> To: "Eric W. Biederman" , Luis Chamberlain , Kees Cook , Joel Granados Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?T?= =?utf-8?q?homas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1711127159; l=2025; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=UNKfueCHNBcmXRHOtTAvJsS3aYDsMC1mej59C9wE9T8=; b=5fseNA5fs6C4QJ0MV+zpKMytFpZsRbKFlT5V4AVL7WSkldAQxOgU2euTtH345bsAfsYEJzy5B /x+5sR4hOklDM03BMarh0YPulieYYd6zNDhfTS2c9l/tI1KVZ75l8bu X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= It is used only twice and those callers are simpler with sysctl_is_perm_empty_ctl_header(). So use this sibling function. This is part of an effort to constify definition of struct ctl_table. For this effort the mutable member 'type' is moved from struct ctl_table to struct ctl_table_header. Unifying the macros sysctl_is_perm_empty_ctl_* makes this easier. Signed-off-by: Thomas Weißschuh --- fs/proc/proc_sysctl.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 37cde0efee57..2f4d4329d83d 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -48,10 +48,8 @@ struct ctl_table_header *register_sysctl_mount_point(const char *path) } EXPORT_SYMBOL(register_sysctl_mount_point); -#define sysctl_is_perm_empty_ctl_table(tptr) \ - (tptr[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) #define sysctl_is_perm_empty_ctl_header(hptr) \ - (sysctl_is_perm_empty_ctl_table(hptr->ctl_table)) + (hptr->ctl_table[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) #define sysctl_set_perm_empty_ctl_header(hptr) \ (hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) #define sysctl_clear_perm_empty_ctl_header(hptr) \ @@ -233,7 +231,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) /* Am I creating a permanently empty directory? */ if (header->ctl_table_size > 0 && - sysctl_is_perm_empty_ctl_table(header->ctl_table)) { + sysctl_is_perm_empty_ctl_header(header)) { if (!RB_EMPTY_ROOT(&dir->root)) return -EINVAL; sysctl_set_perm_empty_ctl_header(dir_h); @@ -1204,7 +1202,7 @@ static bool get_links(struct ctl_dir *dir, struct ctl_table *entry, *link; if (header->ctl_table_size == 0 || - sysctl_is_perm_empty_ctl_table(header->ctl_table)) + sysctl_is_perm_empty_ctl_header(header)) return true; /* Are there links available for every entry in table? */ From patchwork Fri Mar 22 17:05:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13600321 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 0E7F05DF24; Fri, 22 Mar 2024 17:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127167; cv=none; b=qzk+JQebCrZ2UjPV5Zsn9Wf5liE6Is+sNTK6sLu6lTbyh2Gyo94KpYo6EKUuNcLglYOr4MJ99u/ix37EMDIILVAJV/2hGlc2i+KJVJcI2FJXzBsYjCr7FNHfVbsUZAAyw5qU13s3GXMBLJpNFi5MPZ8fg+PIHTmENDvT2Gt77hQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127167; c=relaxed/simple; bh=fbNFIH997ozcRary5wTNHgChd3s3fGTnPrgslom1cXY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JZvNmImzSbVyUyitwwLlvPySwKTqq6ellxeaucY8yHTBRux+07btqY604B2dpFSGzNMtG9xDYJhJKcX/YLB/FnnIBiXamU22F3YXq0MYGTjJaejY7ulMa/26ikK+a+0EyL7+pE//S50ddRC8EDtvGKWSiWqOipnf4QoThouuxBU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=jKrF2+St; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="jKrF2+St" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1711127160; bh=fbNFIH997ozcRary5wTNHgChd3s3fGTnPrgslom1cXY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jKrF2+StoDsBP4OFlzssR6cUtxccJX8wLt3fD5XgcZTbZ4YTpPxfoJwhw1RVXvZuw yFzfxuiJ7n6qsA/1sRDfYs1LoShIGWCRW2Vx/SynyRjRDL9An95HFjfoIKQZt5tLDR jnRhJrO0EORbgl1ZwQFNzoNX2hCuSlJOhifjeOZc= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Fri, 22 Mar 2024 18:05:57 +0100 Subject: [PATCH v2 2/3] sysctl: move sysctl type to ctl_table_header Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240322-sysctl-empty-dir-v2-2-e559cf8ec7c0@weissschuh.net> References: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> In-Reply-To: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> To: "Eric W. Biederman" , Luis Chamberlain , Kees Cook , Joel Granados Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?T?= =?utf-8?q?homas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1711127159; l=3725; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=fbNFIH997ozcRary5wTNHgChd3s3fGTnPrgslom1cXY=; b=VvB9IY168QAX29XiUdGDePb/dx2TNalal2DoM7uVdqJkc8uLWk2TzbHdg5x2xCMvuja8ffKkQ sy55efShkhcAXuVGZFb+Odk8p752NwIqf0Fuq0Bo/J5xtGyJf/S6d+r X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Move the SYSCTL_TABLE_TYPE_{DEFAULT,PERMANENTLY_EMPTY} enums from ctl_table to ctl_table_header. Removing the mutable member is necessary to constify static instances of struct ctl_table. Move the initialization of the sysctl_mount_point type into init_header() where all the other header fields are also initialized. As a side-effect the memory usage of the sysctl core is reduced. Each ctl_table_header instance can manage multiple ctl_table instances and is only allocated when the table is actually registered. This saves 8 bytes of memory per ctl_table on 64bit, 4 due to the enum field itself and 4 due to padding. Signed-off-by: Thomas Weißschuh --- fs/proc/proc_sysctl.c | 10 ++++++---- include/linux/sysctl.h | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index 2f4d4329d83d..fde7a2f773f0 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -31,7 +31,7 @@ static const struct inode_operations proc_sys_dir_operations; /* Support for permanently empty directories */ static struct ctl_table sysctl_mount_point[] = { - {.type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY } + { } }; /** @@ -49,11 +49,11 @@ struct ctl_table_header *register_sysctl_mount_point(const char *path) EXPORT_SYMBOL(register_sysctl_mount_point); #define sysctl_is_perm_empty_ctl_header(hptr) \ - (hptr->ctl_table[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) + (hptr->type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) #define sysctl_set_perm_empty_ctl_header(hptr) \ - (hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) + (hptr->type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) #define sysctl_clear_perm_empty_ctl_header(hptr) \ - (hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_DEFAULT) + (hptr->type = SYSCTL_TABLE_TYPE_DEFAULT) void proc_sys_poll_notify(struct ctl_table_poll *poll) { @@ -208,6 +208,8 @@ static void init_header(struct ctl_table_header *head, node++; } } + if (table == sysctl_mount_point) + sysctl_set_perm_empty_ctl_header(head); } static void erase_header(struct ctl_table_header *head) diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index ee7d33b89e9e..c87f73c06cb9 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -137,17 +137,6 @@ struct ctl_table { void *data; int maxlen; umode_t mode; - /** - * enum type - Enumeration to differentiate between ctl target types - * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations - * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently - * empty directory target to serve - * as mount point. - */ - enum { - SYSCTL_TABLE_TYPE_DEFAULT, - SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY - } type; proc_handler *proc_handler; /* Callback for text formatting */ struct ctl_table_poll *poll; void *extra1; @@ -188,6 +177,17 @@ struct ctl_table_header { struct ctl_dir *parent; struct ctl_node *node; struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */ + /** + * enum type - Enumeration to differentiate between ctl target types + * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations + * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently + * empty directory target to serve + * as mount point. + */ + enum { + SYSCTL_TABLE_TYPE_DEFAULT, + SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY, + } type; }; struct ctl_dir { From patchwork Fri Mar 22 17:05:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13600323 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 0E7AC5D8F0; Fri, 22 Mar 2024 17:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127168; cv=none; b=JPI0sQtZHpucV3EIHUIeA5/J1Y+XqSN94tcH6z67GA/1c0hOZe9rd6EacKHq0+GI7F+bKA8uwboszK5O0vshvWyNP0pASXkX3cT/lgPKft35IVGHVrEOAJMGdmYkbz5A4cj5wc54BjksljO/4UP7JeDQOenImd+rPMcgr/l7K5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711127168; c=relaxed/simple; bh=pKWmydccD60+SiYR6pnPoIGUdo4mn6q9g3JBlH5dGg4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PY/HDjZV+w/J5+LbLCLZQYzWtW5LT4/MJUyQcMscEpf7oNdbkmTEmZs4J89TNYPU/b84H7TFj214bOmOpjZru9aSevR8esoGfB/KecPORZlHYTAmYWEWUryvf6xyuDnep95YnGua10K4rq/ypeOMdCcTjecGqwCIMtgO680ynr0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=TmF/Wipx; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="TmF/Wipx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1711127160; bh=pKWmydccD60+SiYR6pnPoIGUdo4mn6q9g3JBlH5dGg4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=TmF/WipxHKHJanklGRgo/cD7B2NyM0Bu+uXwRPyXWnATzoI9fzXN2RHFXkKEeYuTB Yek9tMJ+eLVIzfLTTOJeOa1NZLa2LoGLgGHy73oZlTIp22u3Nm/kPfA1+DbuScUDKq TOy2rEgVBCFr6TWSgDCd35tEB0kqLoL2VDr2q9Ng= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Fri, 22 Mar 2024 18:05:58 +0100 Subject: [PATCH v2 3/3] sysctl: drop now unnecessary out-of-bounds check Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240322-sysctl-empty-dir-v2-3-e559cf8ec7c0@weissschuh.net> References: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> In-Reply-To: <20240322-sysctl-empty-dir-v2-0-e559cf8ec7c0@weissschuh.net> To: "Eric W. Biederman" , Luis Chamberlain , Kees Cook , Joel Granados Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, =?utf-8?q?T?= =?utf-8?q?homas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1711127159; l=1390; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=pKWmydccD60+SiYR6pnPoIGUdo4mn6q9g3JBlH5dGg4=; b=e2QXcJQf+5J7TWOe5KTw0NSnhPXCh5BNorgGtCKLWzMkZ4ieztJk+0p9kVHGW8OEWOzmZtSik x4WRtM13pd/BmJ+gbkFlP00GzXbeRd6PvpkRta6m+vFWl3C1lhrFNHj X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Remove the now unneeded check for ctl_table_size; it is safe to do so as sysctl_set_perm_empty_ctl_header() does not access the ctl_table member anymore. This also makes the element of sysctl_mount_point unnecessary, so drop it at the same time. Signed-off-by: Thomas Weißschuh --- fs/proc/proc_sysctl.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c index fde7a2f773f0..7c0e27dc3d9d 100644 --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -30,9 +30,7 @@ static const struct file_operations proc_sys_dir_file_operations; static const struct inode_operations proc_sys_dir_operations; /* Support for permanently empty directories */ -static struct ctl_table sysctl_mount_point[] = { - { } -}; +static struct ctl_table sysctl_mount_point[] = { }; /** * register_sysctl_mount_point() - registers a sysctl mount point @@ -232,8 +230,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) return -EROFS; /* Am I creating a permanently empty directory? */ - if (header->ctl_table_size > 0 && - sysctl_is_perm_empty_ctl_header(header)) { + if (sysctl_is_perm_empty_ctl_header(header)) { if (!RB_EMPTY_ROOT(&dir->root)) return -EINVAL; sysctl_set_perm_empty_ctl_header(dir_h);