Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3681789rdh; Tue, 28 Nov 2023 00:19:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFyC7DJzRlZlyrJyEV08bn6uuViz3e6uvKzrE7F2LjvF8S9ja36AUJZVVCHBjBtrNqgghrS X-Received: by 2002:a05:6a00:2410:b0:6b4:c21c:8b56 with SMTP id z16-20020a056a00241000b006b4c21c8b56mr14371245pfh.23.1701159557939; Tue, 28 Nov 2023 00:19:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701159557; cv=none; d=google.com; s=arc-20160816; b=UsEz0STUlGFYGy6SRA0CEOwBPxm1hWsB5gWB1bl5caGaprj+fZg9BDZvrGDpw2pe71 ZLyUbWUa08tisqmajUf4ZBxOdrBRYNBsFwLEfQBySxcgUsHCnOJAF/4EXwNkJzTl6O5h lByXzQDIvGWpXyUWA9qcUp0RAhT0n7Kt+oJEKLndskhHn1knZTVWjJP/m6oLsjO4rKJu 6xnK/ch76icfGIp9dFYidM7i9lelcAf52ep/E8s04bm9KtvsSEKb8pSkKr9nWZKZSxln EGMrzHfj0alJ3eSnCnWIKY6PguH7viIsZa2QH3ffjhygiuA4fMcQD214a1U7r8z+yHRJ lQ7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=79fq3vzVvr28z1/z/6RpLa3lt5gJazFzO9RERrJjqDg=; fh=OdhmpsWDQsjlyU1lesDxzPSAQG2y81xEh5+BX1VSlxw=; b=O9KAv4f+gECs8LQqpKUXeX5Ew11vsu5sVNd/qMnGl2ZCcsaJFybJH3gWpvRjlOnoab A3bLrl0FCn16SH4dLndHPgaUq6vqeWQXiodhyxrzE4V2cLZyXpMmXvqSSiWL5IzXtxiq 5e/xyfV1CqpeXamJv/8eovNCWaBcddZ271Ju1a5qa9A8+uxvJH7Lb/gcPOYBsTILpzq6 cJ6c5iCPHDK57qqKUBYTYMv6I78bIhka7iUgDzDq+KuJsEXLc90iKpcbR2nR2KdG7uMs YY+Wxir3bgIOm+J59rDOE8J6erzsJCV0gA6Wu06daySN8fXdKr1j1U6qewaMs8MGkvax oykA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=W2yx2WsK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id z14-20020a63e10e000000b005c5e216f0f7si1536365pgh.134.2023.11.28.00.19.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 00:19:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=W2yx2WsK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 03D378077992; Tue, 28 Nov 2023 00:18:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344053AbjK1ISc (ORCPT + 99 others); Tue, 28 Nov 2023 03:18:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230044AbjK1ISa (ORCPT ); Tue, 28 Nov 2023 03:18:30 -0500 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19095B0; Tue, 28 Nov 2023 00:18:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1701159511; bh=bALKkCMp4lwFvbZNSnYUsuxiPjBZcOtLPTfv5JH7kEI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=W2yx2WsKSKX+OxGDFmsqxigPuTgF7gXV5uUuM1cjTjiMJJPTck/FA5tPFo/sJKb36 1mbhhZfmKMRRb9N0vtU1BV0SzPDGW6lmu1Tex4fcrh7kV4Qf3z6Vjk5OibJPIdVwai Ccq9HpQqWDDyeLlcWeZ3SdowZByoz58cE27ue6lg= Date: Tue, 28 Nov 2023 09:18:30 +0100 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Joel Granados Cc: Kees Cook , "Gustavo A. R. Silva" , Luis Chamberlain , Iurii Zaikin , Greg Kroah-Hartman , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [PATCH RFC 0/7] sysctl: constify sysctl ctl_tables Message-ID: <475cd5fa-f0cc-4b8b-9e04-458f6d143178@t-8ch.de> References: <20231125-const-sysctl-v1-0-5e881b0e0290@weissschuh.net> <20231127101323.sdnibmf7c3d5ovye@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20231127101323.sdnibmf7c3d5ovye@localhost> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 28 Nov 2023 00:18:46 -0800 (PST) Hi Joel, On 2023-11-27 11:13:23+0100, Joel Granados wrote: > In general I would like to see more clarity with the motivation and I > would also expect some system testing. My comments inline: Thanks for your feedback, response are below. > On Sat, Nov 25, 2023 at 01:52:49PM +0100, Thomas Weißschuh wrote: > > Problem description: > > > > The kernel contains a lot of struct ctl_table throught the tree. > > These are very often 'static' definitions. > > It would be good to mark these tables const to avoid accidental or > > malicious modifications. > It is unclear to me what you mean here with accidental or malicious > modifications. Do you have a specific attack vector in mind? Do you > have an example of how this could happen maliciously? With > accidental, do you mean in proc/sysctl.c? Can you expand more on the > accidental part? There is no specific attack vector I have in mind. The goal is to remove mutable data, especially if it contains pointers, that could be used by an attacker as a step in an exploit. See for example [0], [1]. Accidental can be any out-of-bounds write throughout the kernel. > What happens with the code that modifies these outside the sysctl core? > Like for example in sysctl_route_net_init where the table is modified > depending on the net->user_ns? Would these non-const ctl_table pointers > be ok? would they be handled differently? It is still completely fine to modify the tables before registering, like sysctl_route_net_init is doing. That code should not need any changes. Modifying the table inside the handler function would bypass the validation done when registering so sounds like a bad idea in general. It would still be possible however for a subsystem to do so by just not making their sysctl table const and then modifying the table directly. > > Unfortunately the tables can not be made const because the core > > registration functions expect mutable tables. > > > > This is for two reasons: > > > > 1) sysctl_{set,clear}_perm_empty_ctl_header in the sysctl core modify > > the table. This should be fixable by only modifying the header > > instead of the table itself. > > 2) The table is passed to the handler function as a non-const pointer. > > > > This series is an aproach on fixing reason 2). > So number 2 will be sent in another set? If the initial feedback to the RFC and general process is positive, yes. > > > > Full process: > > > > * Introduce field proc_handler_new for const handlers (this series) > > * Migrate all core handlers to proc_handler_new (this series, partial) > > This can hopefully be done in a big switch, as it only involves > > functions and structures owned by the core sysctl code. > > * Migrate all other sysctl handlers to proc_handler_new. > > * Drop the old proc_handler_field. > > * Fix the sysctl core to not modify the tables anymore. > > * Adapt public sysctl APIs to take "const struct ctl_table *". > > * Teach checkpatch.pl to warn on non-const "struct ctl_table" > > definitions. > > * Migrate definitions of "struct ctl_table" to "const" where applicable. > > > > > > Notes: > > > > Just casting the function pointers around would trigger > > CFI (control flow integrity) warnings. > > > > The name of the new handler "proc_handler_new" is a bit too long messing > > up the alignment of the table definitions. > > Maybe "proc_handler2" or "proc_handler_c" for (const) would be better. > indeed the name does not say much. "_new" looses its meaning quite fast > :) Hopefully somebody comes up with a better name! > In my experience these tree wide modifications are quite tricky. Have you > run any tests to see that everything is as it was? sysctl selftests and > 0-day come to mind. I managed to miss one change in my initial submission: With the hunk below selftests and typing emails work. --- a/fs/proc/proc_sysctl.c +++ b/fs/proc/proc_sysctl.c @@ -1151,7 +1151,7 @@ static int sysctl_check_table(const char *path, struct ctl_table_header *header) else err |= sysctl_check_table_array(path, entry); } - if (!entry->proc_handler) + if (!entry->proc_handler && !entry->proc_handler_new) err |= sysctl_err(path, entry, "No proc_handler"); if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode) > [..] [0] 43a7206b0963 ("driver core: class: make class_register() take a const *") [1] https://lore.kernel.org/lkml/20230930050033.41174-1-wedsonaf@gmail.com/ Thomas