4 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 # Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # AT_BENEATH 0x1000 /* Fail if not under dirfd */
32 # AT_RESOLVE_BENEATH 0x2000 /* As AT_BENEATH, but do not allow
33 # resolve to walk out of dirfd even
41 cat > beneath4.c <<EOF
53 main(int argc, char *argv[])
57 char *cwd, *dir, *obj, *s;
61 "Usage: %s <dir> <test obj> <flag> <expected return>\n",
69 sscanf(argv[3], "%x", &flag);
72 if ((flag & AT_RESOLVE_BENEATH) == 0) {
73 fprintf(stderr, "Flag must be %#x or %#x\n",
74 AT_BENEATH, AT_RESOLVE_BENEATH);
78 if ((fd = open(dir, O_DIRECTORY | O_RDONLY)) == -1)
79 err(1, "open(%s)", dir);
81 if (fstatat(fd, obj, &st, flag) == -1)
88 warn("cwd=%s, top=%s. flag=%0.6x. fstatf(%s) = %2d (expect %2d). %4s",
89 cwd, dir, flag, obj, r, exp, s);
91 return (r == exp ? 0 : errno);
94 cc -o beneath4 -Wall -Wextra -O2 -g beneath4.c || exit 1
97 mkdir -p /tmp/beneath4.dir/a/a
98 touch /tmp/beneath4.dir/a/f
99 ln /tmp/beneath4.dir/a/f /tmp/beneath4.dir/a/c
100 ln -s /tmp/beneath4.dir/a/a /tmp/beneath4.dir/a/d
101 ln -s /tmp/beneath4.dir/a/b /tmp/beneath4.dir/a/e
102 mkfifo /tmp/beneath4.dir/a/fifo
110 #$dir/beneath4 $top a 0x1000 0 || s=1
111 #$dir/beneath4 $top b 0x1000 2 || s=1
112 #$dir/beneath4 $top c 0x1000 0 || s=1
113 #$dir/beneath4 $top d 0x1000 0 || s=1
114 #$dir/beneath4 $top e 0x1000 2 || s=1
115 #$dir/beneath4 $top fifo 0x1000 0 || s=1
116 #$dir/beneath4 $top $top/../../beneath4.d/a/a 0x1000 93 || s=1
117 #$dir/beneath4 $top $top/.. 0x1000 93 || s=1
118 #$dir/beneath4 $top ../a 0x1000 0 || s=1
120 printf "\nAT_RESOLVE_BENEATH\n"
121 $dir/beneath4 $top a 0x2000 0 || s=1
122 $dir/beneath4 $top b 0x2000 2 || s=1
123 $dir/beneath4 $top c 0x2000 0 || s=1
124 $dir/beneath4 $top d 0x2000 93 || s=1
125 $dir/beneath4 $top e 0x2000 93 || s=1
126 $dir/beneath4 $top fifo 0x2000 0 || s=1
127 $dir/beneath4 $top $top/../../beneath4.d/a/a 0x2000 93 || s=1
128 $dir/beneath4 $top $top/.. 0x2000 93 || s=1
129 $dir/beneath4 $top ../a 0x2000 93 || s=1
131 $dir/beneath4 $top ../a 0x0000 0 || s=1