1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#!/bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Test of:
# AT_BENEATH 0x1000 /* Fail if not under dirfd */
# AT_RESOLVE_BENEATH 0x2000 /* As AT_BENEATH, but do not allow
# resolve to walk out of dirfd even
dir=/tmp/beneath4.dir
rm -rf $dir
mkdir -p $dir
here=`pwd`
cd $dir
cat > beneath4.c <<EOF
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
struct stat st;
int exp, fd, flag, r;
char *cwd, *dir, *obj, *s;
if (argc != 5) {
fprintf(stderr,
"Usage: %s <dir> <test obj> <flag> <expected return>\n",
argv[0]);
return (1);
}
cwd = getwd(NULL);
dir = argv[1];
obj = argv[2];
sscanf(argv[3], "%x", &flag);
exp = atoi(argv[4]);
#if 0
if ((flag & AT_RESOLVE_BENEATH) == 0) {
fprintf(stderr, "Flag must be %#x or %#x\n",
AT_BENEATH, AT_RESOLVE_BENEATH);
return (1);
}
#endif
if ((fd = open(dir, O_DIRECTORY | O_RDONLY)) == -1)
err(1, "open(%s)", dir);
if (fstatat(fd, obj, &st, flag) == -1)
r = errno;
else
r = 0;
s = "FAIL";
if (r == exp)
s = "OK";
warn("cwd=%s, top=%s. flag=%0.6x. fstatf(%s) = %2d (expect %2d). %4s",
cwd, dir, flag, obj, r, exp, s);
return (r != exp);
}
EOF
cc -o beneath4 -Wall -Wextra -O2 -g beneath4.c || exit 1
rm beneath4.c
mkdir -p /tmp/beneath4.dir/a/a
touch /tmp/beneath4.dir/a/f
ln /tmp/beneath4.dir/a/f /tmp/beneath4.dir/a/c
ln -s /tmp/beneath4.dir/a/a /tmp/beneath4.dir/a/d
ln -s /tmp/beneath4.dir/a/b /tmp/beneath4.dir/a/e
mkfifo /tmp/beneath4.dir/a/fifo
top=$dir/a
cd $here
s=0
#ls -lR $dir
#echo AT_BENEATH
#$dir/beneath4 $top a 0x1000 0 || s=1
#$dir/beneath4 $top b 0x1000 2 || s=1
#$dir/beneath4 $top c 0x1000 0 || s=1
#$dir/beneath4 $top d 0x1000 0 || s=1
#$dir/beneath4 $top e 0x1000 2 || s=1
#$dir/beneath4 $top fifo 0x1000 0 || s=1
#$dir/beneath4 $top $top/../../beneath4.d/a/a 0x1000 93 || s=1
#$dir/beneath4 $top $top/.. 0x1000 93 || s=1
#$dir/beneath4 $top ../a 0x1000 0 || s=1
printf "\nAT_RESOLVE_BENEATH\n"
$dir/beneath4 $top a 0x2000 0 || s=1
$dir/beneath4 $top b 0x2000 2 || s=1
$dir/beneath4 $top c 0x2000 0 || s=1
$dir/beneath4 $top d 0x2000 93 || s=1
$dir/beneath4 $top e 0x2000 93 || s=1
$dir/beneath4 $top fifo 0x2000 0 || s=1
$dir/beneath4 $top $top/../../beneath4.d/a/a 0x2000 93 || s=1
$dir/beneath4 $top $top/.. 0x2000 93 || s=1
$dir/beneath4 $top ../a 0x2000 93 || s=1
printf "\nNo flag\n"
$dir/beneath4 $top ../a 0x0000 0 || s=1
rm -rf $dir
exit $s
|