1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) 2017 Fujitsu Ltd.
4f08c3bdfSopenharmony_ci# Ported: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci#
6f08c3bdfSopenharmony_ci# This is a regression test about potential uninitialized variable,
7f08c3bdfSopenharmony_ci# the test can crash the buggy kernel, and the bug has been fixed in:
8f08c3bdfSopenharmony_ci#
9f08c3bdfSopenharmony_ci#   commit 38327424b40bcebe2de92d07312c89360ac9229a
10f08c3bdfSopenharmony_ci#   Author: Dan Carpenter <dan.carpenter@oracle.com>
11f08c3bdfSopenharmony_ci#   Date:   Thu Jun 16 15:48:57 2016 +0100
12f08c3bdfSopenharmony_ci#
13f08c3bdfSopenharmony_ci#   KEYS: potential uninitialized variable
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ciTST_SETUP=setup
16f08c3bdfSopenharmony_ciTST_CLEANUP=cleanup
17f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
18f08c3bdfSopenharmony_ciTST_NEEDS_ROOT=1
19f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
20f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="keyctl"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cicheck_keyctl()
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	local nosup
25f08c3bdfSopenharmony_ci	for op in $@; do
26f08c3bdfSopenharmony_ci		nosup=0
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci		if ! keyctl 2>&1 | grep -q "keyctl $op"; then
29f08c3bdfSopenharmony_ci			nosup=1
30f08c3bdfSopenharmony_ci		fi
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci		if [ "$op" = "request2" ]; then
33f08c3bdfSopenharmony_ci			local key=`keyctl request2 user debug:foo bar`
34f08c3bdfSopenharmony_ci			if [ $? -ne 0 ]; then
35f08c3bdfSopenharmony_ci				nosup=1
36f08c3bdfSopenharmony_ci			fi
37f08c3bdfSopenharmony_ci		fi
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci		if [ "$op" = "unlink" ]; then
40f08c3bdfSopenharmony_ci			if ! keyctl unlink $key @s; then
41f08c3bdfSopenharmony_ci				nosup=1
42f08c3bdfSopenharmony_ci			fi
43f08c3bdfSopenharmony_ci		fi
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci		if [ $nosup -ne 0 ]; then
46f08c3bdfSopenharmony_ci			tst_brk TCONF "keyctl operation $op not supported"
47f08c3bdfSopenharmony_ci		fi
48f08c3bdfSopenharmony_ci	done
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cisetup()
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	check_keyctl negate request2 show unlink
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	PATH_KEYSTAT="/proc/key-users"
56f08c3bdfSopenharmony_ci	PATH_KEYQUOTA="/proc/sys/kernel/keys/root_maxbytes"
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if [ ! -f "$PATH_KEYSTAT" ] || [ ! -f "$PATH_KEYQUOTA" ]; then
59f08c3bdfSopenharmony_ci		tst_brk TCONF "'${PATH_KEYSTAT}' or '${PATH_KEYQUOTA}' \
60f08c3bdfSopenharmony_ci			does not exist"
61f08c3bdfSopenharmony_ci	fi
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	ORIG_KEYSZ=`awk -F' +|/' '/ 0:/ {print $8}' $PATH_KEYSTAT`
64f08c3bdfSopenharmony_ci	ORIG_MAXKEYSZ=`cat $PATH_KEYQUOTA`
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cicleanup()
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	if [ -n "$ORIG_MAXKEYSZ" ]; then
70f08c3bdfSopenharmony_ci		echo $ORIG_MAXKEYSZ >$PATH_KEYQUOTA
71f08c3bdfSopenharmony_ci	fi
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cido_test()
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	local quota_excd=0
77f08c3bdfSopenharmony_ci	local maxkeysz=$((ORIG_KEYSZ + 100))
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	while [ $maxkeysz -ge 0 ]
80f08c3bdfSopenharmony_ci	do
81f08c3bdfSopenharmony_ci		echo $maxkeysz >$PATH_KEYQUOTA
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci		keyctl request2 user debug:fred negate @t >temp 2>&1
84f08c3bdfSopenharmony_ci		grep -q -E "quota exceeded" temp
85f08c3bdfSopenharmony_ci		if [ $? -eq 0 ]; then
86f08c3bdfSopenharmony_ci			quota_excd=1
87f08c3bdfSopenharmony_ci			break
88f08c3bdfSopenharmony_ci		fi
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci		local key=`keyctl show | awk '/debug:fred/ {print $1}'`
91f08c3bdfSopenharmony_ci		if [ -z "$key" ]; then
92f08c3bdfSopenharmony_ci			key=`keyctl show | \
93f08c3bdfSopenharmony_ci				awk -F ':' '/inaccessible/ {print $1}'`
94f08c3bdfSopenharmony_ci		fi
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci		if [ -n "$key" ]; then
97f08c3bdfSopenharmony_ci			keyctl unlink $key @s >/dev/null
98f08c3bdfSopenharmony_ci			tst_sleep 50ms
99f08c3bdfSopenharmony_ci		fi
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci		maxkeysz=$((maxkeysz - 4))
102f08c3bdfSopenharmony_ci	done
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	if [ $quota_excd -eq 0 ]; then
105f08c3bdfSopenharmony_ci		tst_res TWARN "Failed to trigger the quota excess"
106f08c3bdfSopenharmony_ci	fi
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	tst_res TPASS "Bug not reproduced"
109f08c3bdfSopenharmony_ci}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci. tst_test.sh
112f08c3bdfSopenharmony_citst_run
113