Debian Codegraph @

tests/ts/misc/canonicalize

raw
1 #!/bin/bash
2
3 # Copyright (C) 2025 Karel Zak <[email protected]>
4 #
5 # This file is part of util-linux.
6 #
7 # This file is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 TS_TOPDIR="${0%/*}/../.."
13 TS_DESC="canonicalize"
14
15 . "$TS_TOPDIR"/functions.sh
16 ts_init "$*"
17
18 ts_skip_nonroot
19
20 ts_check_prog "getent"
21 ts_check_test_command "$TS_CMD_MOUNT"
22 ts_check_test_command "$TS_CMD_UMOUNT"
23 ts_check_test_command "$TS_CMD_SETPRIV"
24 ts_check_test_command "$TS_HELPER_CANONICALIZE"
25
26 grep -q 'nodev[[:space:]]*tmpfs' /proc/filesystems || \
27 ts_skip_subtest "tmpfs unsupported"
28
29 mkdir -p $TS_MOUNTPOINT &> /dev/null
30 $TS_CMD_MOUNT -t tmpfs tmpfs $TS_MOUNTPOINT >> $TS_OUTPUT 2>> $TS_ERRLOG
31 [ $? -eq 0 ] || ts_skip "tmpfs mount failed"
32
33 # reuse the same TS_MOUNTPOINT in all subtests
34 BASE=$TS_MOUNTPOINT
35
36 # setup suid binary
37 TESTPROG=${BASE}/$(basename $TS_HELPER_CANONICALIZE)
38 cp $TS_HELPER_CANONICALIZE $TESTPROG
39 chown root:root $TESTPROG
40 chmod +x,u+s $TESTPROG
41
42 if [ ! -u "$TESTPROG" ] || [ ! -x "$TESTPROG" ]; then
43 $TS_CMD_UMOUNT $BASE
44 ts_skip "cannot setup suid test"
45 fi
46
47
48 ts_init_subtest root-user
49 mkdir -p ${BASE}/aaa/bbb
50 mkdir -p ${BASE}/ccc/ddd
51 ln -s ${BASE}/ccc ${BASE}/aaa/bbb/ccc-sym
52 $TESTPROG ${BASE}/aaa/bbb/ccc-sym/ddd | sed "s:${BASE}::g" >> $TS_OUTPUT 2>> $TS_ERRLOG
53 ts_finalize_subtest
54
55
56 ts_init_subtest non-root-user
57 uid="nobody"
58 if id $uid &>/dev/null; then
59 mkdir -p ${BASE}/root/foo
60 chown -R root:root ${BASE}/root
61 chmod -R o-rwx ${BASE}/root
62 ln -s ${BASE}/root ${BASE}/root-sym
63
64 gid=$(getent passwd "$uid" | cut -d: -f4)
65
66 $TS_CMD_SETPRIV --reuid="$uid" --regid="$gid" --clear-groups \
67 --inh-caps=-all --reset-env \
68 -- $TESTPROG ${BASE}/root-sym/foo \
69 | sed "s:${BASE}::g" >> $TS_OUTPUT 2>> $TS_ERRLOG
70
71 ts_finalize_subtest
72 else
73 ts_skip_subtest "nobody user is missing"
74 fi
75
76
77 # cleanup
78 $TS_CMD_UMOUNT $BASE &> /dev/null
79
80 ts_finalize