File: memfd_secret.c

package info (click to toggle)
strace 6.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 64,424 kB
  • sloc: ansic: 160,349; sh: 9,223; makefile: 3,817; cpp: 944; awk: 353; perl: 267; exp: 62; sed: 9
file content (85 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download | duplicates (3)
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
/*
 * Check decoding of memfd_secret syscall.
 *
 * Copyright (c) 2021 Eugene Syromyatnikov <evgsyr@gmail.com>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "kernel_fcntl.h"
#include "scno.h"

#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

#ifndef RETVAL_INJECTED
# define RETVAL_INJECTED 0
#endif

#ifndef SKIP_IF_PROC_IS_UNAVAILABLE
# define SKIP_IF_PROC_IS_UNAVAILABLE
#endif
#ifndef FD_PATH
# define FD_PATH ""
#endif

#if RETVAL_INJECTED
# define INJ_STR " (INJECTED)\n"
# define INJ_FD_STR FD_PATH " (INJECTED)\n"
#else /* !RETVAL_INJECTED */
# define INJ_STR "\n"
# define INJ_FD_STR "\n"
#endif /* RETVAL_INJECTED */

static const char *errstr;


static long
sys_memfd_secret(unsigned int flags)

{
	static const kernel_ulong_t fill =
		(kernel_ulong_t) 0xbeefcafe00000000ULL;
	kernel_ulong_t arg1 = fill | flags;
	kernel_ulong_t arg2 = fill | 0xdefaeced;
	kernel_ulong_t arg3 = fill | 0xbabefeed;
	kernel_ulong_t arg4 = fill | 0xbadbeefd;
	kernel_ulong_t arg5 = fill | 0xdecaffed;
	kernel_ulong_t arg6 = fill | 0xdeefaced;

	long rc = syscall(__NR_memfd_secret,
			  arg1, arg2, arg3, arg4, arg5, arg6);
	errstr = sprintrc(rc);

	return rc;
}

int
main(void)
{
	SKIP_IF_PROC_IS_UNAVAILABLE;

	static const struct {
		int val;
		const char *str;
	} flags_vals[] = {
		{ ARG_STR(0) },
		{ ARG_STR(O_CLOEXEC) },
		/* O_CLOEXEC is either 0x80000, 0x200000, or 0x400000 */
		{ ARG_STR(0xde97face) },
		{ ARG_STR(O_CLOEXEC|0xde97face) },
	};
	for (size_t i = 0; i < ARRAY_SIZE(flags_vals); i++) {
		long rc = sys_memfd_secret(flags_vals[i].val);
		printf("memfd_secret(%s) = %s%s" INJ_STR,
		       flags_vals[i].str, errstr, rc > 0 ? FD_PATH : "");
	}

	puts("+++ exited with 0 +++");
	return 0;
}