summaryrefslogtreecommitdiff
path: root/src/tests/anonymous-file.c
blob: 79aa3436448d7023a5ae025b655708d42b1c93cd (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2020 Jonas Dreßler.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include <core/meta-anonymous-file.h>

#if defined(HAVE_MEMFD_CREATE)
#define READONLY_SEALS (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)
#endif

static const char *teststring = "test string 1234567890";

static int
test_read_fd_mmap (int         fd,
                   const char *expected_string)
{
  void *mem;
  int string_size;

  string_size = strlen (expected_string) + 1;

  mem = mmap (NULL, string_size, PROT_READ, MAP_PRIVATE, fd, 0);
  g_assert (mem != MAP_FAILED);

  if (strcmp (expected_string, mem) != 0)
    {
      munmap (mem, string_size);
      return FALSE;
    }

  munmap (mem, string_size);
  return TRUE;
}

static int
test_write_fd (int         fd,
               const char *string)
{
  int written_size, string_size;

  string_size = strlen (string) + 1;
  written_size = write (fd, string, string_size);
  if (written_size != string_size)
    return FALSE;

  return TRUE;
}

#if defined(HAVE_MEMFD_CREATE)
static int
test_readonly_seals (int fd)
{
  unsigned int seals;

  seals = fcntl (fd, F_GET_SEALS);
  if (seals == -1)
    return FALSE;

  if (seals != READONLY_SEALS)
    return FALSE;

  return TRUE;
}
#endif

static int
test_write_read (int fd)
{
  g_autofree char *new_string = g_uuid_string_random ();

  if (!test_write_fd (fd, new_string))
    return FALSE;

  if (!test_read_fd_mmap (fd, new_string))
    return FALSE;

  return TRUE;
}

#if defined(HAVE_MEMFD_CREATE)
static int
test_open_write_read (const char *path)
{
  int fd;

  fd = open (path, O_RDWR);
  g_assert (fd != -1);

  if (!test_write_read (fd))
    {
      close (fd);
      return FALSE;
    }

  close (fd);
  return TRUE;
}
#endif

int
main (int    argc,
      char **argv)
{
  MetaAnonymousFile *file;
  int fd = -1, other_fd = -1;
  g_autofree char *fd_path = NULL;

  file = meta_anonymous_file_new (strlen (teststring) + 1,
                                  (const uint8_t *) teststring);
  if (!file)
    {
      g_critical ("%s: Creating file failed", __func__);
      return EXIT_FAILURE;
    }

#if defined(HAVE_MEMFD_CREATE)
  fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_PRIVATE);
  g_assert (fd != -1);
  other_fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_PRIVATE);
  g_assert (other_fd != -1);

  /* When MAPMODE_PRIVATE was used, meta_anonymous_file_open_fd() should always
   * return the same fd. */
  if (other_fd != fd)
    goto fail;

  /* If memfd_create was used and we request a MAPMODE_PRIVATE file, all the
   * readonly seals should be set. */
  if (!test_readonly_seals (fd))
    goto fail;

  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  /* Writing and reading the written data should fail */
  if (test_write_read (fd))
    goto fail;

  /* Instead we should still be reading the teststring */
  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  /* Opening the fd manually in RW mode and writing to it should fail */
  fd_path = g_strdup_printf ("/proc/%d/fd/%d", getpid (), fd);
  if (test_open_write_read (fd_path))
    goto fail;

  /* Instead we should still be reading the teststring */
  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  /* Just to be sure test the other fd, too */
  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  meta_anonymous_file_close_fd (fd);
  meta_anonymous_file_close_fd (fd);


  fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_SHARED);
  g_assert (fd != -1);
  other_fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_SHARED);
  g_assert (other_fd != -1);

  /* The MAPMODE_SHARED fd should not have readonly seals applied */
  if (test_readonly_seals (fd))
    goto fail;

  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  /* Writing and reading the written data should succeed */
  if (!test_write_read (fd))
    goto fail;

  /* The other fd should still read the teststring though */
  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  meta_anonymous_file_close_fd (fd);
  meta_anonymous_file_close_fd (other_fd);


  /* Test an artificial out-of-space situation by setting the maximum file
   * size this process may create to 2 bytes, if memfd_create with
   * MAPMODE_PRIVATE is used, everything should still work (the existing FD
   * should be used). */
  struct rlimit limit = {2, 2};
  if (setrlimit (RLIMIT_FSIZE, &limit) == -1)
    goto fail;

  fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_PRIVATE);
  g_assert (fd != -1);

  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  meta_anonymous_file_close_fd (fd);
#else
  fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_PRIVATE);
  g_assert (fd != -1);
  other_fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_PRIVATE);
  g_assert (other_fd != -1);

  /* Writing and reading the written data should succeed */
  if (!test_write_read (fd))
    goto fail;

  /* The other fd should still read the teststring though */
  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  meta_anonymous_file_close_fd (fd);
  meta_anonymous_file_close_fd (other_fd);


  fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_SHARED);
  g_assert (fd != -1);
  other_fd = meta_anonymous_file_open_fd (file, META_ANONYMOUS_FILE_MAPMODE_SHARED);
  g_assert (other_fd != -1);

  if (!test_read_fd_mmap (fd, teststring))
    goto fail;

  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  /* Writing and reading the written data should succeed */
  if (!test_write_read (fd))
    goto fail;

  /* The other fd should still read the teststring though */
  if (!test_read_fd_mmap (other_fd, teststring))
    goto fail;

  meta_anonymous_file_close_fd (fd);
  meta_anonymous_file_close_fd (other_fd);
#endif

  meta_anonymous_file_free (file);
  return EXIT_SUCCESS;

  fail:
    if (fd > 0)
      meta_anonymous_file_close_fd (fd);
    if (other_fd > 0)
      meta_anonymous_file_close_fd (other_fd);
    meta_anonymous_file_free (file);
    return EXIT_FAILURE;
}