aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/capsicum/copy_file_range.cc
blob: b785eb7f1e97a3bc41d6b2efbc449261617e3f10 (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
#include <sys/types.h>
#include <fcntl.h>

#include <string>

#include "capsicum.h"
#include "capsicum-test.h"
#include "syscalls.h"

#define TOPDIR "cap_copy_file_range"
#define INFILE "infile"
#define OUTFILE "outfile"

/* Test that copy_file_range() checks capabilities correctly.
 * When used without offset arguments, copy_file_range() should
 * require only CAP_READ on the source and CAP_WRITE on the destination
 * file descriptors, respectively.
 * When used with offset arguments, copy_file_range() should
 * additionally require CAP_SEEK.
 */
class CopyFileRangeTest : public ::testing::Test {
 public:
  CopyFileRangeTest() {
    int rc = mkdir(TmpFile(TOPDIR), 0755);
    EXPECT_OK(rc);
    if (rc < 0) {
      EXPECT_EQ(EEXIST, errno);
    }
    wd_ = open(TmpFile(TOPDIR), O_DIRECTORY);
    EXPECT_OK(wd_);
    CreateFile(TmpFile(TOPDIR "/" INFILE));
    CreateFile(TmpFile(TOPDIR "/" OUTFILE));
  }
  ~CopyFileRangeTest() {
    close(wd_);
    unlink(TmpFile(TOPDIR "/" INFILE));
    unlink(TmpFile(TOPDIR "/" OUTFILE));
    rmdir(TmpFile(TOPDIR));
  }

 private:
  void CreateFile(const char *filename) {
    int fd = open(filename, O_CREAT|O_RDWR, 0644);
    const char *contents = "lorem ipsum dolor sit amet";
    EXPECT_OK(fd);
    for (int i = 0; i < 100; i++) {
      EXPECT_OK(write(fd, contents, strlen(contents)));
    }
    close(fd);
  }

 protected:
  int wd_;

  int openInFile(cap_rights_t *rights) {
    int fd = openat(wd_, INFILE, O_RDONLY);
    EXPECT_OK(fd);
    EXPECT_OK(cap_rights_limit(fd, rights));
    return fd;
  }
  int openOutFile(cap_rights_t *rights) {
    int fd = openat(wd_, OUTFILE, O_WRONLY);
    EXPECT_OK(fd);
    EXPECT_OK(cap_rights_limit(fd, rights));
    return fd;
  }
};

TEST_F(CopyFileRangeTest, WriteReadNeg) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_WRITE);
  cap_rights_init(&rights_out, CAP_READ);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, ReadReadNeg) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_READ);
  cap_rights_init(&rights_out, CAP_READ);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, WriteWriteNeg) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_WRITE);
  cap_rights_init(&rights_out, CAP_WRITE);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, ReadWrite) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_READ);
  cap_rights_init(&rights_out, CAP_WRITE);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, ReadSeekWrite) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_READ, CAP_SEEK);
  cap_rights_init(&rights_out, CAP_WRITE);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, ReadWriteSeek) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_READ);
  cap_rights_init(&rights_out, CAP_WRITE, CAP_SEEK);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_NOTCAPABLE(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}

TEST_F(CopyFileRangeTest, ReadSeekWriteSeek) {
  cap_rights_t rights_in, rights_out;

  cap_rights_init(&rights_in, CAP_READ, CAP_SEEK);
  cap_rights_init(&rights_out, CAP_WRITE, CAP_SEEK);

  int fd_in = openInFile(&rights_in);
  int fd_out = openOutFile(&rights_out);
  off_t off_in = 0, off_out = 0;

  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, NULL, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  off_in = 20;
  off_out = 20;
  EXPECT_OK(copy_file_range(fd_in, NULL, fd_out, &off_out, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, NULL, 8, 0));
  EXPECT_OK(copy_file_range(fd_in, &off_in, fd_out, &off_out, 8, 0));
  close(fd_in);
  close(fd_out);
}