Skip to content

Commit 66987ed

Browse files
committed
Test ignoring of unknown bits in open/openat/openat2/chmod mode
1 parent f33aee7 commit 66987ed

File tree

8 files changed

+273
-3
lines changed

8 files changed

+273
-3
lines changed

testcases/kernel/syscalls/chmod/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/chmod05
66
/chmod06
77
/chmod07
8+
/chmod08
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later
2+
* Copyright (C) 2020 Invisible Things Lab
3+
* Michał Kowalczyk <[email protected]>
4+
*/
5+
6+
/*
7+
* DESCRIPTION
8+
* Changes file access permissions using `chmod` with bits outside of 07777 in
9+
* `mode` set and verifies if they were ignored.
10+
*
11+
* WARNING
12+
* The fact that these bits are ignored is not documented (at the time of
13+
* writing). Failure of this test doesn't necessarily mean that a regression
14+
* in Linux was introduced, its intention is to catch accidental interface
15+
* changes and warn kernel developers if that happens.
16+
*/
17+
18+
#include <errno.h>
19+
#include <fcntl.h>
20+
#include <sys/stat.h>
21+
#include <sys/types.h>
22+
23+
#include "tst_test.h"
24+
25+
#define OPEN_MODE 0644
26+
#define CHMOD_MODE (0777 | ~07777)
27+
#define TESTFILE "testfile"
28+
29+
void test_chmod(void)
30+
{
31+
struct stat stat_buf;
32+
33+
TEST(chmod(TESTFILE, CHMOD_MODE));
34+
if (TST_RET == -1) {
35+
tst_res(TFAIL, "chmod(%s, %#o) failed", TESTFILE, CHMOD_MODE);
36+
}
37+
38+
if (stat(TESTFILE, &stat_buf) == -1) {
39+
tst_brk(TFAIL | TTERRNO, "stat failed");
40+
}
41+
42+
mode_t expected = S_IFREG | (CHMOD_MODE & 07777);
43+
if (stat_buf.st_mode == expected) {
44+
tst_res(TPASS, "Unknown mode bits were ignored as expected",
45+
TESTFILE, CHMOD_MODE);
46+
} else {
47+
tst_res(TFAIL, "%s: Incorrect mode 0%04o, expected 0%04o",
48+
TESTFILE, stat_buf.st_mode, expected);
49+
}
50+
}
51+
52+
void setup(void)
53+
{
54+
int fd;
55+
56+
fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, OPEN_MODE);
57+
SAFE_CLOSE(fd);
58+
}
59+
60+
static struct tst_test test = {
61+
.needs_tmpdir = 1,
62+
.setup = setup,
63+
.test_all = test_chmod,
64+
};

testcases/kernel/syscalls/open/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/open12_child
1414
/open13
1515
/open14
16+
/open15
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later
2+
* Copyright (C) 2020 Invisible Things Lab
3+
* Michał Kowalczyk <[email protected]>
4+
*/
5+
6+
/*
7+
* DESCRIPTION
8+
* Creates a file using `open` with bits outside of 07777 in `mode` set and
9+
* verifies if they were ignored.
10+
*
11+
* WARNING
12+
* The fact that these bits are ignored is not documented (at the time of
13+
* writing). Failure of this test doesn't necessarily mean that a regression
14+
* in Linux was introduced, its intention is to catch accidental interface
15+
* changes and warn kernel developers if that happens.
16+
*/
17+
18+
#include <errno.h>
19+
#include <fcntl.h>
20+
#include <sys/stat.h>
21+
#include <sys/types.h>
22+
23+
#include "tst_test.h"
24+
25+
#define TEST_FILE "testfile"
26+
27+
static int fd;
28+
29+
static struct tcase {
30+
char *filename;
31+
int flags;
32+
mode_t mode;
33+
} tcases[] = {
34+
{TEST_FILE, O_RDWR | O_CREAT, 0644 | ~07777},
35+
{TEST_FILE, 0, ~07777}
36+
};
37+
38+
static void verify_open(unsigned int n)
39+
{
40+
struct tcase *tc = &tcases[n];
41+
struct stat buf;
42+
43+
TEST(open(tc->filename, tc->flags, tc->mode));
44+
fd = TST_RET;
45+
if (fd == -1) {
46+
tst_res(TFAIL, "Cannot open the file");
47+
} else {
48+
tst_res(TPASS, "Unknown mode bits were ignored as expected");
49+
SAFE_CLOSE(fd);
50+
}
51+
}
52+
53+
static void setup(void)
54+
{
55+
}
56+
57+
static void cleanup(void)
58+
{
59+
}
60+
61+
static struct tst_test test = {
62+
.tcnt = ARRAY_SIZE(tcases),
63+
.needs_tmpdir = 1,
64+
.setup = setup,
65+
.cleanup = cleanup,
66+
.test = verify_open,
67+
};

testcases/kernel/syscalls/openat/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/openat02
33
/openat02_child
44
/openat03
5+
/openat04
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later
2+
* Copyright (C) 2020 Invisible Things Lab
3+
* Michał Kowalczyk <[email protected]>
4+
*/
5+
6+
/*
7+
* DESCRIPTION
8+
* Creates a file using `openat` with bits outside of 07777 in `mode` set and
9+
* verifies if they were ignored.
10+
*
11+
* WARNING
12+
* The fact that these bits are ignored is not documented (at the time of
13+
* writing). Failure of this test doesn't necessarily mean that a regression
14+
* in Linux was introduced, its intention is to catch accidental interface
15+
* changes and warn kernel developers if that happens.
16+
*/
17+
18+
#include <errno.h>
19+
#include <fcntl.h>
20+
#include <sys/stat.h>
21+
#include <sys/types.h>
22+
23+
#include "tst_test.h"
24+
25+
#define TEST_FILE "testfile"
26+
27+
static int fd;
28+
29+
static struct tcase {
30+
char *filename;
31+
int flags;
32+
mode_t mode;
33+
} tcases[] = {
34+
{TEST_FILE, O_RDWR | O_CREAT, 0644 | ~07777},
35+
{TEST_FILE, 0, ~07777}
36+
};
37+
38+
static void verify_open(unsigned int n)
39+
{
40+
struct tcase *tc = &tcases[n];
41+
struct stat buf;
42+
43+
TEST(openat(AT_FDCWD, tc->filename, tc->flags, tc->mode));
44+
fd = TST_RET;
45+
if (fd == -1) {
46+
tst_res(TFAIL, "Cannot open the file");
47+
} else {
48+
tst_res(TPASS, "Unknown mode bits were ignored as expected");
49+
SAFE_CLOSE(fd);
50+
}
51+
}
52+
53+
static void setup(void)
54+
{
55+
}
56+
57+
static void cleanup(void)
58+
{
59+
}
60+
61+
static struct tst_test test = {
62+
.tcnt = ARRAY_SIZE(tcases),
63+
.needs_tmpdir = 1,
64+
.setup = setup,
65+
.cleanup = cleanup,
66+
.test = verify_open,
67+
};
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
openat201
2-
openat202
3-
openat203
1+
/openat201
2+
/openat202
3+
/openat203
4+
/openat204
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later
2+
* Copyright (C) 2020 Invisible Things Lab
3+
* Michał Kowalczyk <[email protected]>
4+
*/
5+
6+
/*
7+
* DESCRIPTION
8+
* Creates a file using `openat2` with bits outside of 07777 in `mode` set and
9+
* verifies if they were ignored.
10+
*
11+
* WARNING
12+
* The fact that these bits are ignored is not documented (at the time of
13+
* writing). Failure of this test doesn't necessarily mean that a regression
14+
* in Linux was introduced, its intention is to catch accidental interface
15+
* changes and warn kernel developers if that happens.
16+
*/
17+
18+
#include <errno.h>
19+
#include <fcntl.h>
20+
#include <sys/stat.h>
21+
#include <sys/types.h>
22+
23+
#include "tst_test.h"
24+
#include "lapi/openat2.h"
25+
26+
#define TEST_FILE "testfile"
27+
28+
static int fd;
29+
30+
static struct tcase {
31+
char *filename;
32+
int flags;
33+
mode_t mode;
34+
} tcases[] = {
35+
{TEST_FILE, O_RDWR | O_CREAT, 0644 | ~07777},
36+
{TEST_FILE, 0, ~07777}
37+
};
38+
39+
static void verify_open(unsigned int n)
40+
{
41+
struct tcase *tc = &tcases[n];
42+
struct stat buf;
43+
44+
TEST(openat2(AT_FDCWD, tc->filename, tc->flags, tc->mode));
45+
fd = TST_RET;
46+
if (fd == -1) {
47+
tst_res(TFAIL, "Cannot open the file");
48+
} else {
49+
tst_res(TPASS, "Unknown mode bits were ignored as expected");
50+
SAFE_CLOSE(fd);
51+
}
52+
}
53+
54+
static void setup(void)
55+
{
56+
}
57+
58+
static void cleanup(void)
59+
{
60+
}
61+
62+
static struct tst_test test = {
63+
.tcnt = ARRAY_SIZE(tcases),
64+
.needs_tmpdir = 1,
65+
.setup = setup,
66+
.cleanup = cleanup,
67+
.test = verify_open,
68+
};

0 commit comments

Comments
 (0)