1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #if !defined _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20
21 #include <err.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/uio.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30
31 #include "../includes/common.h"
32
33 #define SYSCHK(x) \
34 ({ \
35 typeof(x) __res = (x); \
36 if (__res == (typeof(x)) - 1) err(1, "SYSCHK(" #x ")"); \
37 __res; \
38 })
39
40 static char *data;
41
child_fn(void)42 static int child_fn(void) {
43 int pipe_fds[2];
44 SYSCHK(pipe(pipe_fds));
45 struct iovec iov = {.iov_base = data, .iov_len = 0x1000};
46 SYSCHK(vmsplice(pipe_fds[1], &iov, 1, 0));
47 SYSCHK(munmap(data, 0x1000));
48 sleep(2);
49 char buf[0x1000];
50 SYSCHK(read(pipe_fds[0], buf, 0x1000));
51 printf("read string from child: %s\n", buf);
52
53 // check if buf has been altered by parent process
54 if (strcmp("BORING DATA", buf) == 0) {
55 return EXIT_SUCCESS;
56 }
57 if (strcmp("THIS IS SECRET", buf) == 0) {
58 return EXIT_VULNERABLE;
59 }
60 return EXIT_FAILURE;
61 }
62
main(void)63 int main(void) {
64 if (posix_memalign((void **)&data, 0x1000, 0x1000)) errx(1, "posix_memalign()");
65 strcpy(data, "BORING DATA");
66
67 pid_t child = SYSCHK(fork());
68 if (child == 0) {
69 exit(child_fn());
70 }
71
72 sleep(1);
73 strcpy(data, "THIS IS SECRET");
74
75 int status;
76 SYSCHK(waitpid(child, &status, 0));
77 printf("child WEXITSTATUS(status) => %d\n", WEXITSTATUS(status));
78 return WEXITSTATUS(status);
79 }
80