about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/tst-openat2.c
blob: 6936b9fcef8425eed3eb5570063b2fc3531df7aa (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Linux openat2 tests.
   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* openat2 is only support in LFS mode.  */
#define _FILE_OFFSET_BITS 64

#include <array_length.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <support/check.h>
#include <support/temp_file.h>
#include <support/xunistd.h>

static int dir_fd;

static void
do_prepare (int argc, char *argv[])
{
  char *temp_dir = support_create_temp_directory ("tst-openat2");
  dir_fd = xopen (temp_dir, O_RDONLY | O_DIRECTORY, 0);
}
#define PREPARE do_prepare

static int
do_test_struct (void)
{
  static struct struct_test
  {
    struct open_how_ext
    {
      struct open_how inner;
      int extra1;
      int extra2;
      int extra3;
    } arg;
    size_t size;
    int err;
  } tests[] =
  {
    {
      /* Zero size.  */
      .arg.inner.flags = O_RDONLY,
      .size = 0,
      .err = EINVAL,
    },
    {
      /* Normal struct.  */
      .arg.inner.flags = O_RDONLY,
      .size = sizeof (struct open_how),
    },
    {
      /* Larger struct, zeroed out the unused values.  */
      .arg.inner.flags = O_RDONLY,
      .size = sizeof (struct open_how_ext),
    },
    {
      /* Larger struct, non-zeroed out the unused values.  */
      .arg.inner.flags = O_RDONLY,
      .arg.extra1 = 0xdeadbeef,
      .size = sizeof (struct open_how_ext),
      .err = E2BIG,
    },
    {
      /* Larger struct, non-zeroed out the unused values.  */
      .arg.inner.flags = O_RDONLY,
      .arg.extra2 = 0xdeadbeef,
      .size = sizeof (struct open_how_ext),
      .err = E2BIG,
    },
  };

  for (struct struct_test *t = tests; t != array_end (tests); t++)
    {
      int fd = openat2 (AT_FDCWD, ".", (struct open_how *) &t->arg, t->size);
      if (fd == -1 && errno == ENOSYS)
	FAIL_UNSUPPORTED ("openat2 is not supported by the kernel");

      if (t->err != 0)
	{
	  TEST_COMPARE (fd, -1);
	  TEST_COMPARE (errno, t->err);
	}
      else
	TEST_VERIFY (fd >= 0);
    }

  return 0;
}

static int
do_test_flags (void)
{
  static struct flag_test
  {
    struct open_how how;
    int err;
  } tests[] =
  {
    /* O_TMPFILE is incompatible with O_PATH and O_CREAT.  */
    { .how.flags = O_TMPFILE | O_PATH | O_RDWR, .err = EINVAL },
    { .how.flags = O_TMPFILE | O_CREAT | O_RDWR, .err = EINVAL },

    /* O_PATH only permits certain other flags to be set ...  */
    { .how.flags = O_PATH | O_CLOEXEC },
    { .how.flags = O_PATH | O_DIRECTORY },
    { .how.flags = O_PATH | O_NOFOLLOW },
    /* ... and others are absolutely not permitted. */
    { .how.flags = O_PATH | O_RDWR, .err = EINVAL },
    { .how.flags = O_PATH | O_CREAT, .err = EINVAL },
    { .how.flags = O_PATH | O_EXCL, .err = EINVAL },
    { .how.flags = O_PATH | O_NOCTTY, .err = EINVAL },
    { .how.flags = O_PATH | O_DIRECT, .err = EINVAL },

    /* ->mode must only be set with O_{CREAT,TMPFILE}. */
    { .how.flags = O_RDONLY, .how.mode = 0600, .err = EINVAL },
    { .how.flags = O_PATH,   .how.mode = 0600, .err = EINVAL },
    { .how.flags = O_CREAT,  .how.mode = 0600 },
    { .how.flags = O_TMPFILE | O_RDWR, .how.mode = 0600 },
    /* ->mode must only contain 0777 bits. */
    { .how.flags = O_CREAT, .how.mode = 0xFFFF, .err = EINVAL },
    { .how.flags = O_CREAT, .how.mode = 0xC000000000000000ULL, .err = EINVAL },
    { .how.flags = O_TMPFILE | O_RDWR, .how.mode = 0x1337, .err = EINVAL },
    { .how.flags = O_TMPFILE | O_RDWR, .how.mode = 0x0000A00000000000ULL,
      .err = EINVAL },

    /* ->resolve flags must not conflict. */
    { .how.flags = O_RDONLY,
      .how.resolve = RESOLVE_BENEATH | RESOLVE_IN_ROOT,
      .err = EINVAL },

    /* ->resolve must only contain RESOLVE_* flags.  */
    { .how.flags = O_RDONLY,
      .how.resolve = 0x1337, .err = EINVAL },
    { .how.flags = O_CREAT,
      .how.resolve = 0x1337, .err = EINVAL },
    { .how.flags = O_TMPFILE | O_RDWR,
      .how.resolve = 0x1337, .err = EINVAL },
    { .how.flags = O_PATH,
      .how.resolve = 0x1337, .err = EINVAL },

    /* currently unknown upper 32 bit rejected.  */
    { .how.flags = O_RDONLY | (1ULL << 63),
      .how.resolve = 0, .err = EINVAL },
  };

  for (struct flag_test *t = tests; t != array_end (tests); t++)
    {
      char *path;
      if (t->how.flags & O_CREAT)
	{
	  int temp_fd = create_temp_file ("tst-openat2.", &path);
	  TEST_VERIFY_EXIT (temp_fd != -1);
	  xunlink (path);
	}
      else
	path = (char *) ".";

      int fd = openat2 (AT_FDCWD, path, &t->how, sizeof (struct open_how));
      if (fd != 0 && errno == EOPNOTSUPP)
	{
	  /* Skip the testcase if FS does not support the operation (e.g.
	     valid O_TMPFILE on NFS).  */
	  continue;
	}

      if (t->err != 0)
	{
	  TEST_COMPARE (fd, -1);
	  TEST_COMPARE (errno, t->err);
	}
      else
	TEST_VERIFY (fd >= 0);
    }

  return 0;
}

static int
do_test_basic (void)
{
  int fd;

  fd = openat2 (dir_fd,
		"some-file",
		&(struct open_how)
		{
		  .flags = O_CREAT|O_RDWR|O_EXCL,
		  .mode = 0666,
		},
		sizeof (struct open_how));
  TEST_VERIFY (fd != -1);

  xwrite (fd, "hello", 5);

  /* Before closing the file, try using this file descriptor to open
     another file.  This must fail.  */
  {
    int fd2 = openat2 (fd,
		       "should-not-work",
		       &(struct open_how)
		       {
			 .flags = O_CREAT|O_RDWR|O_EXCL,
			 .mode = 0666,
		       },
		       sizeof (struct open_how));
    TEST_COMPARE (fd2, -1);
    TEST_COMPARE (errno, ENOTDIR);
  }

  /* Remove the created file.  */
  int cwdfd = xopen (".", O_RDONLY | O_DIRECTORY, 0);
  TEST_COMPARE (fchdir (dir_fd), 0);
  xunlink ("some-file");
  TEST_COMPARE (fchdir (cwdfd), 0);

  xclose (dir_fd);
  xclose (cwdfd);

  fd = openat2 (dir_fd,
		"some-file",
		&(struct open_how)
		{
		  .flags = O_CREAT|O_RDWR|O_EXCL,
		  .mode = 0666,
		},
		sizeof (struct open_how));
  TEST_COMPARE (fd, -1);
  TEST_COMPARE (errno, EBADF);

  return 0;
}

static int
do_test (void)
{
  do_test_struct ();
  do_test_flags ();
  do_test_basic ();

  return 0;
}

#include <support/test-driver.c>