1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2021 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Check for potential issues in writev() if the first iovec entry is NULL
10*49cdfc7eSAndroid Build Coastguard Worker * and the next one is not present in RAM. This can result in a brief window
11*49cdfc7eSAndroid Build Coastguard Worker * where writev() first writes uninitialized data into the file (possibly
12*49cdfc7eSAndroid Build Coastguard Worker * exposing internal kernel structures) and then overwrites it with the real
13*49cdfc7eSAndroid Build Coastguard Worker * iovec contents later.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker /*
17*49cdfc7eSAndroid Build Coastguard Worker * Bugs fixed in:
18*49cdfc7eSAndroid Build Coastguard Worker * commit d4690f1e1cdabb4d61207b6787b1605a0dc0aeab
19*49cdfc7eSAndroid Build Coastguard Worker * Author: Al Viro <[email protected]>
20*49cdfc7eSAndroid Build Coastguard Worker * Date: Fri Sep 16 00:11:45 2016 +0100
21*49cdfc7eSAndroid Build Coastguard Worker *
22*49cdfc7eSAndroid Build Coastguard Worker * fix iov_iter_fault_in_readable()
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_atomic.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fuzzy_sync.h"
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK_SIZE 256
31*49cdfc7eSAndroid Build Coastguard Worker #define BUF_SIZE (2 * CHUNK_SIZE)
32*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
33*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE MNTPOINT "/test_file"
34*49cdfc7eSAndroid Build Coastguard Worker #define MAPFILE MNTPOINT "/map_file"
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker static unsigned char buf[BUF_SIZE], *map_ptr;
37*49cdfc7eSAndroid Build Coastguard Worker static int mapfd = -1, writefd = -1, readfd = -1;
38*49cdfc7eSAndroid Build Coastguard Worker static int written;
39*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fzsync_pair fzsync_pair;
40*49cdfc7eSAndroid Build Coastguard Worker struct iovec iov[5];
41*49cdfc7eSAndroid Build Coastguard Worker
setup(void)42*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker int i;
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < BUF_SIZE; i++)
47*49cdfc7eSAndroid Build Coastguard Worker buf[i] = i & 0xff;
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker mapfd = SAFE_OPEN(MAPFILE, O_CREAT|O_RDWR|O_TRUNC, 0644);
50*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, mapfd, buf, BUF_SIZE);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_init(&fzsync_pair);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker
thread_run(void * arg)55*49cdfc7eSAndroid Build Coastguard Worker static void *thread_run(void *arg)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_b(&fzsync_pair)) {
58*49cdfc7eSAndroid Build Coastguard Worker writefd = SAFE_OPEN(TEMPFILE, O_CREAT|O_WRONLY|O_TRUNC, 0644);
59*49cdfc7eSAndroid Build Coastguard Worker written = BUF_SIZE;
60*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_wait_b(&fzsync_pair);
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker /*
63*49cdfc7eSAndroid Build Coastguard Worker * Do *NOT* preload the data using MAP_POPULATE or touching
64*49cdfc7eSAndroid Build Coastguard Worker * the mapped range. We're testing whether writev() handles
65*49cdfc7eSAndroid Build Coastguard Worker * fault-in correctly.
66*49cdfc7eSAndroid Build Coastguard Worker */
67*49cdfc7eSAndroid Build Coastguard Worker map_ptr = SAFE_MMAP(NULL, BUF_SIZE, PROT_READ, MAP_SHARED,
68*49cdfc7eSAndroid Build Coastguard Worker mapfd, 0);
69*49cdfc7eSAndroid Build Coastguard Worker iov[1].iov_base = map_ptr;
70*49cdfc7eSAndroid Build Coastguard Worker iov[1].iov_len = CHUNK_SIZE;
71*49cdfc7eSAndroid Build Coastguard Worker iov[3].iov_base = map_ptr + CHUNK_SIZE;
72*49cdfc7eSAndroid Build Coastguard Worker iov[3].iov_len = CHUNK_SIZE;
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_b(&fzsync_pair);
75*49cdfc7eSAndroid Build Coastguard Worker tst_atomic_store(writev(writefd, iov, ARRAY_SIZE(iov)),
76*49cdfc7eSAndroid Build Coastguard Worker &written);
77*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_b(&fzsync_pair);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(map_ptr, BUF_SIZE);
80*49cdfc7eSAndroid Build Coastguard Worker map_ptr = NULL;
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(writefd);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker return arg;
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
run(void)87*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker int total_read;
90*49cdfc7eSAndroid Build Coastguard Worker unsigned char readbuf[BUF_SIZE + 1];
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_reset(&fzsync_pair, thread_run);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker while (tst_fzsync_run_a(&fzsync_pair)) {
95*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_wait_a(&fzsync_pair);
96*49cdfc7eSAndroid Build Coastguard Worker readfd = SAFE_OPEN(TEMPFILE, O_RDONLY);
97*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_start_race_a(&fzsync_pair);
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker for (total_read = 0; total_read < tst_atomic_load(&written);) {
100*49cdfc7eSAndroid Build Coastguard Worker total_read += SAFE_READ(0, readfd, readbuf+total_read,
101*49cdfc7eSAndroid Build Coastguard Worker BUF_SIZE + 1 - total_read);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_end_race_a(&fzsync_pair);
105*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(readfd);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker if (total_read > BUF_SIZE)
108*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "writev() wrote too much data");
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker if (total_read <= 0)
111*49cdfc7eSAndroid Build Coastguard Worker continue;
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker if (memcmp(readbuf, buf, total_read)) {
114*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "writev() wrote invalid data");
115*49cdfc7eSAndroid Build Coastguard Worker return;
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "writev() handles page fault-in correctly");
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)122*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker if (map_ptr && map_ptr != MAP_FAILED)
125*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(map_ptr, BUF_SIZE);
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker if (mapfd >= 0)
128*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(mapfd);
129*49cdfc7eSAndroid Build Coastguard Worker
130*49cdfc7eSAndroid Build Coastguard Worker if (readfd >= 0)
131*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(readfd);
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker if (writefd >= 0)
134*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(writefd);
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker tst_fzsync_pair_cleanup(&fzsync_pair);
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
140*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
141*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
142*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
143*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
144*49cdfc7eSAndroid Build Coastguard Worker .all_filesystems = 1,
145*49cdfc7eSAndroid Build Coastguard Worker .min_cpus = 2,
146*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
147*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
148*49cdfc7eSAndroid Build Coastguard Worker .max_runtime = 75,
149*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
150*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "d4690f1e1cda"},
151*49cdfc7eSAndroid Build Coastguard Worker {}
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker };
154