1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2019 FUJITSU LIMITED. All rights reserved. 4# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz> 5# Author: Yang Xu<xuyang2018.jy@cn.fujitsu.com> 6# 7# Test for these regressions causing buffer overflow when writing into 8# /proc/sys/fs/file-max: 9# 7f2923c4f73f ("sysctl: handle overflow in proc_get_long") 10# 32a5ad9c2285 ("sysctl: handle overflow for file-max") 11# 12# This bug has been fixed in 9002b21465fa ("kernel/sysctl.c: fix 13# out-of-bounds access when setting file-max") 14# 15# We test in sysctl02.sh setting 2^64, 2^64-1, 2^63 and 0. 16 17TST_TESTFUNC=do_test 18TST_SETUP=setup 19TST_CLEANUP=cleanup 20TST_CNT=4 21TST_NEEDS_ROOT=1 22TST_NEEDS_CMDS="sysctl" 23TST_NEEDS_KCONFIGS="CONFIG_SYSCTL=y, CONFIG_PROC_FS=y" 24sys_name="fs.file-max" 25sys_file="/proc/sys/fs/file-max" 26 27. tst_test.sh 28 29setup() 30{ 31 orig_value=$(cat "$sys_file") 32} 33 34do_test() 35{ 36 case $1 in 37 1) sysctl_test_overflow 18446744073709551616 ;; 38 2) sysctl_test_overflow 18446744073709551615 ;; 39 3) sysctl_test_overflow 9223372036854775808 ;; 40 4) sysctl_test_zero ;; 41 esac 42} 43 44sysctl_test_overflow() 45{ 46 local test_value="$1" 47 local old_value="$(cat $sys_file)" 48 49 tst_res TINFO "trying to set $sys_name=$test_value" 50 sysctl -w -q $sys_name=$test_value 2>/dev/null 51 local new_value="$(cat $sys_file)" 52 53 if [ "$new_value" = "$old_value" ]; then 54 tst_res TPASS "$sys_file keeps old value ($old_value)" 55 else 56 tst_res TFAIL "$sys_file overflows and is set to $new_value" 57 fi 58 cleanup 59} 60 61sysctl_test_zero() 62{ 63 tst_check_kconfigs "CONFIG_KALLSYMS=y,CONFIG_KALLSYMS_ALL=y,CONFIG_KASAN=y" \ 64 || tst_brk TCONF "kconfig doesn't meet test's requirement!" 65 66 ROD sysctl -w -q $sys_name=0 67 68 if dmesg | grep -q "KASAN: global-out-of-bounds in __do_proc_doulongvec_minmax"; then 69 tst_res TFAIL "$sys_file is set 0 and trigger a KASAN error" 70 else 71 tst_res TPASS "$sys_file is set 0 and doesn't trigger a KASAN error" 72 fi 73} 74 75cleanup() 76{ 77 [ -n "$orig_value" ] && sysctl -w -q $sys_name=$orig_value 78} 79 80tst_run 81