|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | + * Copyright (C) 2020 Invisible Things Lab |
| 3 | + * Michał Kowalczyk <mkow@invisiblethingslab.com> |
| 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 | +}; |
0 commit comments