1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include <sys/uio.h>
11 #include <sys/wait.h>
12 #include <unistd.h>
13 
14 #include <crazy_linker.h>
15 
16 #include "test_util.h"
17 
18 #define PARENT_ADDRESS 0x20000000
19 #define CHILD_ADDRESS 0x20134000
20 
21 typedef void (*FunctionPtr)();
22 
main()23 int main() {
24 
25   if (!crazy_system_can_share_relro()) {
26     fprintf(stderr, "WARNING: Test ignored due to broken kernel!!\n");
27     return 0;
28   }
29 
30   crazy_context_t* context = crazy_context_create();
31 
32   RelroLibrary foo;
33 
34   int pipes[2];
35   if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes) < 0)
36     Panic("Could not create socket pair: %s", strerror(errno));
37 
38   pid_t child = fork();
39   if (child < 0)
40     Panic("Could not fork test program!");
41 
42   if (child == 0) {
43     // In the child.
44     crazy_context_set_load_address(context, CHILD_ADDRESS);
45     foo.Init("libfoo_with_relro.so", context);
46 
47     printf("Child waiting for foo relro fd\n");
48 
49     foo.ReceiveRelroInfo(pipes[0]);
50     foo.UseSharedRelro(context);
51 
52     printf("RELRO used in child process\n");
53 
54     CheckRelroMaps(1);
55 
56     FunctionPtr foo_func;
57     if (!crazy_library_find_symbol(
58              foo.library, "Foo", reinterpret_cast<void**>(&foo_func)))
59       Panic("Could not find 'Foo' in library");
60 
61     printf("Calling Foo()\n");
62     (*foo_func)();
63 
64     printf("Foo called, exiting\n");
65 
66     exit(0);
67 
68   } else {
69     // In the parent.
70 
71     // Load at fixed address to simplify testing.
72     crazy_context_set_load_address(context, PARENT_ADDRESS);
73     foo.Init("libfoo_with_relro.so", context);
74 
75     printf("Library loaded\n");
76 
77     printf("Parent enabling foo RELRO sharing\n");
78 
79     foo.CreateSharedRelro(context, CHILD_ADDRESS);
80     foo.SendRelroInfo(pipes[1]);
81 
82     printf("Relocated RELRO sent to child\n");
83 
84     CheckRelroMaps(0);
85 
86     printf("Parent waiting for child\n");
87 
88     // Wait for child to complete.
89     int status;
90     waitpid(child, &status, 0);
91 
92     if (WIFSIGNALED(status))
93       Panic("Child terminated by signal!!\n");
94     else if (WIFEXITED(status)) {
95       int child_status = WEXITSTATUS(status);
96       if (child_status != 0)
97         Panic("Child terminated with status=%d\n", child_status);
98     } else
99       Panic("Child exited for unknown reason!!\n");
100   }
101 
102   crazy_context_destroy(context);
103   return 0;
104 }
105