Skip to content

Commit 4b3a170

Browse files
committed
Implement name_to_handle_at and open_by_handle_at syscalls decoding
* file_handle.c: New file. * Makefile.am (strace_SOURCES): Add it. * linux/dummy.h (sys_name_to_handle_at, sys_open_by_handle_at): Remove stub aliases. * pathtrace.c (pathtrace_match): Add SEN_name_to_handle_at. * tests/file_handle.c: New file. * tests/file_handle.test: New test. * tests/Makefile.am (check_PROGRAMS): Add file_handle. (TESTS): Add file_handle.test. * tests/.gitignore: Add file_handle.
1 parent 60d7524 commit 4b3a170

File tree

8 files changed

+261
-2
lines changed

8 files changed

+261
-2
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ strace_SOURCES = \
6363
fcntl.c \
6464
fetch_seccomp_fprog.c \
6565
file.c \
66+
file_handle.c \
6667
flock.c \
6768
futex.c \
6869
get_robust_list.c \

file_handle.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 2015 Dmitry V. Levin <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. The name of the author may not be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include "defs.h"
29+
30+
#ifndef MAX_HANDLE_SZ
31+
# define MAX_HANDLE_SZ 128
32+
#endif
33+
34+
typedef struct {
35+
unsigned int handle_bytes;
36+
int handle_type;
37+
} file_handle_header;
38+
39+
SYS_FUNC(name_to_handle_at)
40+
{
41+
file_handle_header h;
42+
const long addr = tcp->u_arg[2];
43+
44+
if (entering(tcp)) {
45+
/* dirfd */
46+
print_dirfd(tcp, tcp->u_arg[0]);
47+
48+
/* pathname */
49+
printpath(tcp, tcp->u_arg[1]);
50+
tprints(", ");
51+
52+
/* handle */
53+
if (umove_or_printaddr(tcp, addr, &h)) {
54+
tprints(", ");
55+
56+
/* mount_id */
57+
printaddr(tcp->u_arg[3]);
58+
tprints(", ");
59+
60+
/* flags */
61+
printflags(at_flags, tcp->u_arg[4], "AT_???");
62+
63+
return RVAL_DECODED;
64+
}
65+
tprintf("{handle_bytes=%u", h.handle_bytes);
66+
67+
/*
68+
* Abusing tcp->auxstr as a temporary storage.
69+
* Will be used and cleared on syscall exit.
70+
*/
71+
tcp->auxstr = (void *) (unsigned long) h.handle_bytes;
72+
73+
return 0;
74+
} else {
75+
unsigned int i = (unsigned long) tcp->auxstr;
76+
77+
if ((!syserror(tcp) || EOVERFLOW == tcp->u_error)
78+
&& !umove(tcp, addr, &h)) {
79+
unsigned char f_handle[MAX_HANDLE_SZ];
80+
81+
if (i != h.handle_bytes)
82+
tprintf(" => %u", h.handle_bytes);
83+
if (!syserror(tcp)) {
84+
tprintf(", handle_type=%d", h.handle_type);
85+
if (h.handle_bytes > MAX_HANDLE_SZ)
86+
h.handle_bytes = MAX_HANDLE_SZ;
87+
if (!umoven(tcp, addr + sizeof(h), h.handle_bytes,
88+
f_handle)) {
89+
tprints(", f_handle=0x");
90+
for (i = 0; i < h.handle_bytes; ++i)
91+
tprintf("%02x", f_handle[i]);
92+
}
93+
}
94+
}
95+
tprints("}, ");
96+
tcp->auxstr = NULL;
97+
98+
/* mount_id */
99+
printnum_int(tcp, tcp->u_arg[3], "%d");
100+
tprints(", ");
101+
102+
/* flags */
103+
printflags(at_flags, tcp->u_arg[4], "AT_???");
104+
}
105+
return 0;
106+
}
107+
108+
SYS_FUNC(open_by_handle_at)
109+
{
110+
file_handle_header h;
111+
const long addr = tcp->u_arg[1];
112+
113+
/* mount_fd */
114+
printfd(tcp, tcp->u_arg[0]);
115+
tprints(", ");
116+
117+
/* handle */
118+
if (!umove_or_printaddr(tcp, addr, &h)) {
119+
unsigned char f_handle[MAX_HANDLE_SZ];
120+
121+
tprintf("{handle_bytes=%u, handle_type=%d",
122+
h.handle_bytes, h.handle_type);
123+
if (h.handle_bytes > MAX_HANDLE_SZ)
124+
h.handle_bytes = MAX_HANDLE_SZ;
125+
if (!umoven(tcp, addr + sizeof(h), h.handle_bytes, &f_handle)) {
126+
unsigned int i;
127+
128+
tprints(", f_handle=0x");
129+
for (i = 0; i < h.handle_bytes; ++i)
130+
tprintf("%02x", f_handle[i]);
131+
}
132+
tprints("}");
133+
}
134+
tprints(", ");
135+
136+
/* flags */
137+
tprint_open_modes(tcp->u_arg[2]);
138+
139+
return RVAL_DECODED;
140+
}

linux/dummy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#endif
3333

3434
/* still unfinished */
35-
#define sys_name_to_handle_at printargs
36-
#define sys_open_by_handle_at printargs
3735
#define sys_sysfs printargs
3836
#define sys_vm86 printargs
3937
#define sys_vm86old printargs

pathtrace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ pathtrace_match(struct tcb *tcp)
178178
case SEN_inotify_add_watch:
179179
case SEN_mkdirat:
180180
case SEN_mknodat:
181+
case SEN_name_to_handle_at:
181182
case SEN_newfstatat:
182183
case SEN_openat:
183184
case SEN_pipe2:

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ eventfd
99
execve
1010
execveat
1111
fanotify_mark
12+
file_handle
1213
filter-unavailable
1314
getdents
1415
getdents64

tests/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ check_PROGRAMS = \
2222
execve \
2323
execveat \
2424
fanotify_mark \
25+
file_handle \
2526
filter-unavailable \
2627
getdents \
2728
getdents64 \
@@ -122,6 +123,7 @@ TESTS = \
122123
execve.test \
123124
execveat.test \
124125
fanotify_mark.test \
126+
file_handle.test \
125127
filter-unavailable.test \
126128
getdents.test \
127129
getdents64.test \

tests/file_handle.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2015 Dmitry V. Levin <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. The name of the author may not be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifdef HAVE_CONFIG_H
29+
# include "config.h"
30+
#endif
31+
32+
#include <alloca.h>
33+
#include <assert.h>
34+
#include <errno.h>
35+
#include <fcntl.h>
36+
#include <stdio.h>
37+
38+
#ifdef MAX_HANDLE_SZ
39+
40+
int
41+
main(void)
42+
{
43+
struct file_handle *handle =
44+
alloca(sizeof(struct file_handle) + MAX_HANDLE_SZ);
45+
const int dirfd = AT_FDCWD;
46+
const int flags = AT_SYMLINK_FOLLOW;
47+
int mount_id;
48+
unsigned int i;
49+
50+
handle->handle_bytes = 0;
51+
52+
if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags | 1) != -1
53+
|| EINVAL != errno) {
54+
perror("name_to_handle_at");
55+
return 77;
56+
}
57+
printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
58+
", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (Invalid argument)\n",
59+
&mount_id);
60+
61+
if (name_to_handle_at(dirfd, ".", handle, &mount_id, flags) != -1
62+
|| EOVERFLOW != errno) {
63+
perror("name_to_handle_at");
64+
return 77;
65+
}
66+
printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
67+
", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW"
68+
" (Value too large for defined data type)\n",
69+
handle->handle_bytes, &mount_id);
70+
71+
assert(!name_to_handle_at(dirfd, ".", handle, &mount_id, flags));
72+
printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
73+
", handle_type=%d, f_handle=0x",
74+
handle->handle_bytes, handle->handle_type);
75+
for (i = 0; i < handle->handle_bytes; ++i)
76+
printf("%02x", handle->f_handle[i]);
77+
printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
78+
79+
assert(open_by_handle_at(-1, handle, O_RDONLY | O_DIRECTORY));
80+
printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=1, f_handle=0x",
81+
handle->handle_bytes);
82+
for (i = 0; i < handle->handle_bytes; ++i)
83+
printf("%02x", handle->f_handle[i]);
84+
printf("}, O_RDONLY|O_DIRECTORY) = -1 %s\n",
85+
EPERM == errno ? "EPERM (Operation not permitted)" :
86+
EINVAL == errno ? "EINVAL (Invalid argument)" :
87+
"EBADF (Bad file descriptor)");
88+
89+
puts("+++ exited with 0 +++");
90+
return 0;
91+
}
92+
93+
#else
94+
95+
int
96+
main(void)
97+
{
98+
return 77;
99+
}
100+
101+
#endif

tests/file_handle.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
# Check name_to_handle_at and open_by_handle_at syscalls decoding.
4+
5+
. "${srcdir=.}/init.sh"
6+
7+
run_prog > /dev/null
8+
9+
OUT="$LOG.out"
10+
run_strace -ename_to_handle_at,open_by_handle_at $args > "$OUT"
11+
12+
match_diff "$OUT" "$LOG"
13+
rm -f "$OUT"
14+
15+
exit 0

0 commit comments

Comments
 (0)