• Wen Yang's avatar
    sysctl: move the extra1/2 boundary check of u8 to sysctl_check_table_array · b5ffbd13
    Wen Yang authored
    Move boundary checking for proc_dou8ved_minmax into module loading, thereby
    reporting errors in advance. And add a kunit test case ensuring the
    boundary check is done correctly.
    
    The boundary check in proc_dou8vec_minmax done to the extra elements in
    the ctl_table struct is currently performed at runtime. This allows buggy
    kernel modules to be loaded normally without any errors only to fail
    when used.
    
    This is a buggy example module:
    	#include <linux/kernel.h>
    	#include <linux/module.h>
    	#include <linux/sysctl.h>
    
    	static struct ctl_table_header *_table_header = NULL;
    	static unsigned char _data = 0;
    	struct ctl_table table[] = {
    		{
    			.procname       = "foo",
    			.data           = &_data,
    			.maxlen         = sizeof(u8),
    			.mode           = 0644,
    			.proc_handler   = proc_dou8vec_minmax,
    			.extra1         = SYSCTL_ZERO,
    			.extra2         = SYSCTL_ONE_THOUSAND,
    		},
    	};
    
    	static int init_demo(void) {
    		_table_header = register_sysctl("kernel", table);
    		if (!_table_header)
    			return -ENOMEM;
    
    		return 0;
    	}
    
    	module_init(init_demo);
    	MODULE_LICENSE("GPL");
    
    And this is the result:
            # insmod test.ko
            # cat /proc/sys/kernel/foo
            cat: /proc/sys/kernel/foo: Invalid argument
    Suggested-by: default avatarJoel Granados <j.granados@samsung.com>
    Signed-off-by: default avatarWen Yang <wen.yang@linux.dev>
    Cc: Luis Chamberlain <mcgrof@kernel.org>
    Cc: Kees Cook <keescook@chromium.org>
    Cc: Joel Granados <j.granados@samsung.com>
    Cc: Eric W. Biederman <ebiederm@xmission.com>
    Cc: Christian Brauner <brauner@kernel.org>
    Cc: linux-kernel@vger.kernel.org
    Reviewed-by: default avatarJoel Granados <j.granados@samsung.com>
    Signed-off-by: default avatarJoel Granados <j.granados@samsung.com>
    b5ffbd13
sysctl.c 53.6 KB