Home | History | Annotate | Download | only in keyctl
      1 #!/bin/sh
      2 #
      3 # Copyright (c) 2017 Fujitsu Ltd.
      4 # Ported: Guangwen Feng <fenggw-fnst (at] cn.fujitsu.com>
      5 #
      6 # This program is free software; you can redistribute it and/or modify
      7 # it under the terms of the GNU General Public License as published by
      8 # the Free Software Foundation; either version 2 of the License, or
      9 # (at your option) any later version.
     10 #
     11 # This program is distributed in the hope that it would be useful,
     12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 # GNU General Public License for more details.
     15 #
     16 # You should have received a copy of the GNU General Public License
     17 # along with this program, if not, see <http://www.gnu.org/licenses/>.
     18 #
     19 # This is a regression test about potential uninitialized variable,
     20 # the test can crash the buggy kernel, and the bug has been fixed in:
     21 #
     22 #   commit 38327424b40bcebe2de92d07312c89360ac9229a
     23 #   Author: Dan Carpenter <dan.carpenter (at] oracle.com>
     24 #   Date:   Thu Jun 16 15:48:57 2016 +0100
     25 #
     26 #   KEYS: potential uninitialized variable
     27 #
     28 
     29 TST_SETUP=setup
     30 TST_CLEANUP=cleanup
     31 TST_TESTFUNC=do_test
     32 TST_NEEDS_ROOT=1
     33 TST_NEEDS_TMPDIR=1
     34 TST_NEEDS_CMDS="keyctl"
     35 . tst_test.sh
     36 
     37 check_keyctl()
     38 {
     39 	local nosup
     40 	for op in $@; do
     41 		nosup=0
     42 
     43 		if ! keyctl 2>&1 | grep -q "keyctl $op"; then
     44 			nosup=1
     45 		fi
     46 
     47 		if [ "$op" = "request2" ]; then
     48 			local key=`keyctl request2 user debug:foo bar`
     49 			if [ $? -ne 0 ]; then
     50 				nosup=1
     51 			fi
     52 		fi
     53 
     54 		if [ "$op" = "unlink" ]; then
     55 			if ! keyctl unlink $key @s; then
     56 				nosup=1
     57 			fi
     58 		fi
     59 
     60 		if [ $nosup -ne 0 ]; then
     61 			tst_brk TCONF "keyctl operation $op not supported"
     62 		fi
     63 	done
     64 }
     65 
     66 setup()
     67 {
     68 	check_keyctl negate request2 show unlink
     69 
     70 	PATH_KEYSTAT="/proc/key-users"
     71 	PATH_KEYQUOTA="/proc/sys/kernel/keys/root_maxbytes"
     72 
     73 	if [ ! -f "$PATH_KEYSTAT" ] || [ ! -f "$PATH_KEYQUOTA" ]; then
     74 		tst_brk TCONF "'${PATH_KEYSTAT}' or '${PATH_KEYQUOTA}' \
     75 			does not exist"
     76 	fi
     77 
     78 	ORIG_KEYSZ=`awk -F' +|/' '/ 0:/ {print $8}' $PATH_KEYSTAT`
     79 	ORIG_MAXKEYSZ=`cat $PATH_KEYQUOTA`
     80 }
     81 
     82 cleanup()
     83 {
     84 	if [ -n "$ORIG_MAXKEYSZ" ]; then
     85 		echo $ORIG_MAXKEYSZ >$PATH_KEYQUOTA
     86 	fi
     87 }
     88 
     89 do_test()
     90 {
     91 	local quota_excd=0
     92 	local maxkeysz=$((ORIG_KEYSZ + 100))
     93 
     94 	while [ $maxkeysz -ge 0 ]
     95 	do
     96 		echo $maxkeysz >$PATH_KEYQUOTA
     97 
     98 		keyctl request2 user debug:fred negate @t >temp 2>&1
     99 		grep -q -E "quota exceeded" temp
    100 		if [ $? -eq 0 ]; then
    101 			quota_excd=1
    102 			break
    103 		fi
    104 
    105 		local key=`keyctl show | awk '/debug:fred/ {print $1}'`
    106 		if [ -z "$key" ]; then
    107 			key=`keyctl show | \
    108 				awk -F ':' '/inaccessible/ {print $1}'`
    109 		fi
    110 
    111 		if [ -n "$key" ]; then
    112 			keyctl unlink $key @s >/dev/null
    113 			tst_sleep 50ms
    114 		fi
    115 
    116 		((maxkeysz -= 4))
    117 	done
    118 
    119 	if [ $quota_excd -eq 0 ]; then
    120 		tst_res TWARN "Failed to trigger the quota excess"
    121 	fi
    122 
    123 	tst_res TPASS "Bug not reproduced"
    124 }
    125 
    126 tst_run
    127