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" 24 25sys_name="fs.file-max" 26sys_file="/proc/sys/fs/file-max" 27 28setup() 29{ 30 orig_value=$(cat "$sys_file") 31} 32 33do_test() 34{ 35 case $1 in 36 1) sysctl_test_overflow 18446744073709551616 ;; 37 2) sysctl_test_overflow 18446744073709551615 ;; 38 3) sysctl_test_overflow 9223372036854775808 ;; 39 4) sysctl_test_zero ;; 40 esac 41} 42 43sysctl_test_overflow() 44{ 45 local test_value="$1" 46 local old_value="$(cat $sys_file)" 47 48 tst_res TINFO "trying to set $sys_name=$test_value" 49 sysctl -w -q $sys_name=$test_value 2>/dev/null 50 local new_value="$(cat $sys_file)" 51 52 if [ "$new_value" = "$old_value" ]; then 53 tst_res TPASS "$sys_file keeps old value ($old_value)" 54 else 55 tst_res TFAIL "$sys_file overflows and is set to $new_value" 56 fi 57 cleanup 58} 59 60sysctl_test_zero() 61{ 62 tst_check_kconfigs "CONFIG_KALLSYMS=y,CONFIG_KALLSYMS_ALL=y,CONFIG_KASAN=y" \ 63 || tst_brk TCONF "kconfig doesn't meet test's requirement!" 64 65 ROD sysctl -w -q $sys_name=0 66 67 if dmesg | grep -q "KASAN: global-out-of-bounds in __do_proc_doulongvec_minmax"; then 68 tst_res TFAIL "$sys_file is set 0 and trigger a KASAN error" 69 else 70 tst_res TPASS "$sys_file is set 0 and doesn't trigger a KASAN error" 71 fi 72} 73 74cleanup() 75{ 76 [ -n "$orig_value" ] && sysctl -w -q $sys_name=$orig_value 77} 78 79. tst_test.sh 80tst_run 81