Mercurial
annotate mrjunejune/latex_renderer.c @ 244:b8aa08503378
[tools] Add sandboxed online LaTeX editor
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 03 Aug 2026 16:56:25 -0700 |
| parents | |
| children | 70f2a3dafc1c |
| rev | line source |
|---|---|
|
244
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
1 #include "mrjunejune/latex_renderer.h" |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
2 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
3 #ifdef __linux__ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
4 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
5 #include <dirent.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
6 #include <errno.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
7 #include <fcntl.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
8 #include <linux/audit.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
9 #include <linux/filter.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
10 #include <linux/landlock.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
11 #include <linux/seccomp.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
12 #include <limits.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
13 #include <poll.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
14 #include <signal.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
15 #include <stdbool.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
16 #include <stddef.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
17 #include <stdio.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
18 #include <stdlib.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
19 #include <string.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
20 #include <sys/prctl.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
21 #include <sys/resource.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
22 #include <sys/stat.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
23 #include <sys/syscall.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
24 #include <sys/types.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
25 #include <sys/wait.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
26 #include <time.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
27 #include <unistd.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
28 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
29 #ifndef LANDLOCK_ACCESS_FS_REFER |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
30 #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
31 #endif |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
32 #ifndef LANDLOCK_ACCESS_FS_TRUNCATE |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
33 #define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
34 #endif |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
35 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
36 #if defined(__x86_64__) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
37 #define LATEX_AUDIT_ARCH AUDIT_ARCH_X86_64 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
38 #elif defined(__aarch64__) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
39 #define LATEX_AUDIT_ARCH AUDIT_ARCH_AARCH64 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
40 #else |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
41 #define LATEX_AUDIT_ARCH 0 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
42 #endif |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
43 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
44 #define LATEX_DIAGNOSTICS_MAX_BYTES (16 * 1024) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
45 #define LATEX_CPU_TIMEOUT_SECONDS 5 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
46 #define LATEX_WALL_TIMEOUT_SECONDS 8 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
47 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
48 static char *duplicate_message(const char *message) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
49 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
50 size_t length = strlen(message); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
51 char *copy = malloc(length + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
52 if (copy) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
53 memcpy(copy, message, length + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
54 return copy; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
55 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
56 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
57 static Latex_Render_Result result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
58 Latex_Render_Status status, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
59 const char *message) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
60 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
61 return (Latex_Render_Result){ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
62 .status = status, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
63 .diagnostics = duplicate_message(message), |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
64 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
65 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
66 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
67 static bool write_all(int fd, const uint8_t *data, size_t size) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
68 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
69 while (size > 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
70 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
71 ssize_t written = write(fd, data, size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
72 if (written < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
73 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
74 if (errno == EINTR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
75 continue; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
76 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
77 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
78 data += written; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
79 size -= (size_t)written; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
80 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
81 return true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
82 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
83 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
84 static int remove_tree(const char *path, unsigned depth) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
85 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
86 if (depth > 16) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
87 return -1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
88 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
89 struct stat status; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
90 if (lstat(path, &status) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
91 return errno == ENOENT ? 0 : -1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
92 if (!S_ISDIR(status.st_mode)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
93 return unlink(path); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
94 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
95 DIR *directory = opendir(path); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
96 if (!directory) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
97 return -1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
98 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
99 int result = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
100 struct dirent *entry; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
101 while ((entry = readdir(directory)) != NULL) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
102 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
103 if (strcmp(entry->d_name, ".") == 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
104 strcmp(entry->d_name, "..") == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
105 continue; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
106 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
107 char child[1024]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
108 int length = snprintf( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
109 child, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
110 sizeof(child), |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
111 "%s/%s", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
112 path, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
113 entry->d_name); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
114 if (length < 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
115 (size_t)length >= sizeof(child) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
116 remove_tree(child, depth + 1) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
117 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
118 result = -1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
119 break; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
120 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
121 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
122 closedir(directory); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
123 return result == 0 ? rmdir(path) : result; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
124 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
125 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
126 static bool set_limit(int resource, rlim_t value) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
127 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
128 struct rlimit limit = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
129 .rlim_cur = value, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
130 .rlim_max = value, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
131 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
132 return setrlimit(resource, &limit) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
133 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
134 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
135 static bool add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
136 int ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
137 const char *path, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
138 uint64_t access, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
139 bool required) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
140 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
141 int path_fd = open(path, O_PATH | O_CLOEXEC); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
142 if (path_fd < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
143 return !required && errno == ENOENT; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
144 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
145 struct stat status; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
146 bool ok = fstat(path_fd, &status) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
147 if (ok && !S_ISDIR(status.st_mode)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
148 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
149 access &= LANDLOCK_ACCESS_FS_EXECUTE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
150 LANDLOCK_ACCESS_FS_READ_FILE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
151 LANDLOCK_ACCESS_FS_WRITE_FILE; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
152 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
153 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
154 struct landlock_path_beneath_attr rule = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
155 .allowed_access = access, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
156 .parent_fd = path_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
157 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
158 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
159 ok = syscall( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
160 SYS_landlock_add_rule, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
161 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
162 LANDLOCK_RULE_PATH_BENEATH, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
163 &rule, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
164 0) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
165 close(path_fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
166 return ok; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
167 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
168 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
169 static bool apply_filesystem_sandbox(const char *job_directory) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
170 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
171 int abi = syscall( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
172 SYS_landlock_create_ruleset, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
173 NULL, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
174 0, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
175 LANDLOCK_CREATE_RULESET_VERSION); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
176 if (abi < 1) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
177 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
178 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
179 uint64_t read_access = |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
180 LANDLOCK_ACCESS_FS_EXECUTE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
181 LANDLOCK_ACCESS_FS_READ_FILE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
182 LANDLOCK_ACCESS_FS_READ_DIR; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
183 uint64_t write_access = |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
184 LANDLOCK_ACCESS_FS_WRITE_FILE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
185 LANDLOCK_ACCESS_FS_REMOVE_DIR | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
186 LANDLOCK_ACCESS_FS_REMOVE_FILE | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
187 LANDLOCK_ACCESS_FS_MAKE_CHAR | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
188 LANDLOCK_ACCESS_FS_MAKE_DIR | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
189 LANDLOCK_ACCESS_FS_MAKE_REG | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
190 LANDLOCK_ACCESS_FS_MAKE_SOCK | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
191 LANDLOCK_ACCESS_FS_MAKE_FIFO | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
192 LANDLOCK_ACCESS_FS_MAKE_BLOCK | |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
193 LANDLOCK_ACCESS_FS_MAKE_SYM; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
194 if (abi >= 2) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
195 write_access |= LANDLOCK_ACCESS_FS_REFER; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
196 if (abi >= 3) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
197 write_access |= LANDLOCK_ACCESS_FS_TRUNCATE; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
198 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
199 struct landlock_ruleset_attr ruleset = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
200 .handled_access_fs = read_access | write_access, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
201 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
202 int ruleset_fd = syscall( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
203 SYS_landlock_create_ruleset, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
204 &ruleset, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
205 sizeof(ruleset), |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
206 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
207 if (ruleset_fd < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
208 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
209 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
210 const char *read_only_directories[] = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
211 "/usr", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
212 "/lib", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
213 "/lib64", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
214 "/bin", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
215 "/var/lib/texmf", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
216 "/etc/texmf", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
217 "/etc/fonts", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
218 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
219 bool ok = true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
220 for (size_t i = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
221 ok && i < sizeof(read_only_directories) / sizeof(read_only_directories[0]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
222 i++) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
223 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
224 ok = add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
225 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
226 read_only_directories[i], |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
227 read_access, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
228 false); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
229 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
230 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
231 ok = add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
232 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
233 "/etc/ld.so.cache", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
234 LANDLOCK_ACCESS_FS_READ_FILE, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
235 false); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
236 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
237 ok = add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
238 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
239 "/dev/null", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
240 LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
241 true); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
242 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
243 ok = add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
244 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
245 "/dev/urandom", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
246 LANDLOCK_ACCESS_FS_READ_FILE, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
247 false); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
248 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
249 ok = add_landlock_path_rule( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
250 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
251 job_directory, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
252 read_access | write_access, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
253 true); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
254 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
255 ok = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
256 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
257 ok = syscall( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
258 SYS_landlock_restrict_self, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
259 ruleset_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
260 0) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
261 close(ruleset_fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
262 return ok; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
263 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
264 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
265 static bool apply_network_sandbox(void) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
266 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
267 if (LATEX_AUDIT_ARCH == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
268 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
269 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
270 struct sock_filter filters[64]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
271 size_t count = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
272 #define ADD_FILTER(value) filters[count++] = (struct sock_filter)value |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
273 #define DENY_SYSCALL(number) \ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
274 do { \ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
275 ADD_FILTER(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, number, 0, 1)); \ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
276 ADD_FILTER(BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | EPERM)); \ |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
277 } while (0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
278 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
279 ADD_FILTER(BPF_STMT( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
280 BPF_LD | BPF_W | BPF_ABS, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
281 offsetof(struct seccomp_data, arch))); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
282 ADD_FILTER(BPF_JUMP( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
283 BPF_JMP | BPF_JEQ | BPF_K, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
284 LATEX_AUDIT_ARCH, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
285 1, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
286 0)); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
287 ADD_FILTER(BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS)); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
288 ADD_FILTER(BPF_STMT( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
289 BPF_LD | BPF_W | BPF_ABS, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
290 offsetof(struct seccomp_data, nr))); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
291 DENY_SYSCALL(__NR_socket); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
292 DENY_SYSCALL(__NR_socketpair); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
293 DENY_SYSCALL(__NR_connect); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
294 DENY_SYSCALL(__NR_accept); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
295 DENY_SYSCALL(__NR_accept4); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
296 DENY_SYSCALL(__NR_bind); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
297 DENY_SYSCALL(__NR_listen); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
298 DENY_SYSCALL(__NR_sendto); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
299 DENY_SYSCALL(__NR_recvfrom); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
300 DENY_SYSCALL(__NR_sendmsg); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
301 DENY_SYSCALL(__NR_recvmsg); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
302 ADD_FILTER(BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
303 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
304 struct sock_fprog program = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
305 .len = (unsigned short)count, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
306 .filter = filters, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
307 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
308 #undef DENY_SYSCALL |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
309 #undef ADD_FILTER |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
310 return prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &program) == 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
311 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
312 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
313 static void configure_child_environment(const char *job_directory) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
314 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
315 clearenv(); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
316 setenv("HOME", job_directory, 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
317 setenv("TMPDIR", job_directory, 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
318 setenv("TEXMFVAR", job_directory, 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
319 setenv("TEXMFCONFIG", job_directory, 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
320 setenv("openin_any", "p", 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
321 setenv("openout_any", "p", 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
322 setenv("shell_escape", "0", 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
323 setenv("SOURCE_DATE_EPOCH", "0", 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
324 setenv("LANG", "C.UTF-8", 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
325 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
326 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
327 static void close_inherited_descriptors(void) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
328 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
329 #ifdef SYS_close_range |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
330 if (syscall(SYS_close_range, 3U, UINT_MAX, 0) == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
331 return; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
332 #endif |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
333 struct rlimit limit; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
334 rlim_t maximum = 65536; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
335 if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
336 limit.rlim_cur != RLIM_INFINITY && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
337 limit.rlim_cur < maximum) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
338 maximum = limit.rlim_cur; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
339 for (int fd = 3; fd < (int)maximum; fd++) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
340 close(fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
341 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
342 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
343 static void run_compiler_child( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
344 const char *compiler, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
345 const char *job_directory, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
346 int output_fd) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
347 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
348 if (dup2(output_fd, STDOUT_FILENO) < 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
349 dup2(output_fd, STDERR_FILENO) < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
350 _exit(120); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
351 if (output_fd != STDOUT_FILENO && output_fd != STDERR_FILENO) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
352 close(output_fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
353 close_inherited_descriptors(); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
354 if (setpgid(0, 0) != 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
355 prctl(PR_SET_PDEATHSIG, SIGKILL) != 0 || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
356 getppid() == 1) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
357 _exit(119); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
358 if (chdir(job_directory) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
359 _exit(121); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
360 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
361 configure_child_environment(job_directory); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
362 if (!set_limit(RLIMIT_CPU, LATEX_CPU_TIMEOUT_SECONDS) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
363 !set_limit(RLIMIT_AS, 768 * 1024 * 1024) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
364 !set_limit(RLIMIT_FSIZE, LATEX_PDF_MAX_BYTES) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
365 !set_limit(RLIMIT_NOFILE, 64) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
366 !set_limit(RLIMIT_NPROC, 1)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
367 _exit(122); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
368 if (!apply_filesystem_sandbox(job_directory) || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
369 !apply_network_sandbox()) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
370 _exit(123); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
371 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
372 char *const arguments[] = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
373 (char *)compiler, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
374 "-interaction=nonstopmode", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
375 "-halt-on-error", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
376 "-no-shell-escape", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
377 "-file-line-error", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
378 "-output-directory=.", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
379 "document.tex", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
380 NULL, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
381 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
382 execv(compiler, arguments); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
383 _exit(errno == ENOENT ? 127 : 126); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
384 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
385 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
386 static double elapsed_seconds( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
387 const struct timespec *start, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
388 const struct timespec *now) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
389 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
390 return (double)(now->tv_sec - start->tv_sec) + |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
391 (double)(now->tv_nsec - start->tv_nsec) / 1000000000.0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
392 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
393 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
394 static int wait_for_compiler( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
395 pid_t child, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
396 int output_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
397 char *diagnostics, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
398 size_t diagnostics_size, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
399 bool *timed_out) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
400 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
401 int flags = fcntl(output_fd, F_GETFL, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
402 if (flags >= 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
403 fcntl(output_fd, F_SETFL, flags | O_NONBLOCK); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
404 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
405 size_t used = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
406 int child_status = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
407 bool child_done = false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
408 struct timespec start; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
409 clock_gettime(CLOCK_MONOTONIC, &start); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
410 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
411 while (!child_done) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
412 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
413 char buffer[2048]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
414 ssize_t count; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
415 while ((count = read(output_fd, buffer, sizeof(buffer))) > 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
416 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
417 size_t available = diagnostics_size - used - 1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
418 size_t copy_size = (size_t)count < available ? (size_t)count : available; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
419 if (copy_size > 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
420 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
421 memcpy(diagnostics + used, buffer, copy_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
422 used += copy_size; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
423 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
424 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
425 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
426 pid_t waited = waitpid(child, &child_status, WNOHANG); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
427 if (waited == child) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
428 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
429 child_done = true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
430 break; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
431 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
432 if (waited < 0 && errno != EINTR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
433 break; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
434 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
435 struct timespec now; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
436 clock_gettime(CLOCK_MONOTONIC, &now); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
437 if (elapsed_seconds(&start, &now) >= LATEX_WALL_TIMEOUT_SECONDS) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
438 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
439 *timed_out = true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
440 kill(-child, SIGKILL); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
441 while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
442 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
443 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
444 child_done = true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
445 break; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
446 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
447 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
448 struct pollfd poll_fd = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
449 .fd = output_fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
450 .events = POLLIN, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
451 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
452 poll(&poll_fd, 1, 50); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
453 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
454 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
455 char buffer[2048]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
456 ssize_t count; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
457 while ((count = read(output_fd, buffer, sizeof(buffer))) > 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
458 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
459 size_t available = diagnostics_size - used - 1; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
460 size_t copy_size = (size_t)count < available ? (size_t)count : available; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
461 if (copy_size > 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
462 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
463 memcpy(diagnostics + used, buffer, copy_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
464 used += copy_size; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
465 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
466 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
467 diagnostics[used] = '\0'; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
468 kill(-child, SIGKILL); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
469 return child_status; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
470 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
471 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
472 static bool read_pdf( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
473 const char *path, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
474 uint8_t **data, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
475 size_t *size) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
476 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
477 int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
478 if (fd < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
479 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
480 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
481 struct stat status; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
482 bool ok = fstat(fd, &status) == 0 && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
483 S_ISREG(status.st_mode) && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
484 status.st_size > 4 && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
485 status.st_size <= LATEX_PDF_MAX_BYTES; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
486 uint8_t *content = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
487 if (ok) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
488 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
489 content = malloc((size_t)status.st_size); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
490 ok = content != NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
491 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
492 size_t offset = 0; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
493 while (ok && offset < (size_t)status.st_size) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
494 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
495 ssize_t count = read( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
496 fd, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
497 content + offset, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
498 (size_t)status.st_size - offset); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
499 if (count < 0 && errno == EINTR) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
500 continue; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
501 if (count <= 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
502 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
503 ok = false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
504 break; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
505 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
506 offset += (size_t)count; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
507 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
508 close(fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
509 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
510 if (!ok || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
511 memcmp(content, "%PDF-", 5) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
512 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
513 free(content); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
514 return false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
515 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
516 *data = content; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
517 *size = offset; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
518 return true; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
519 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
520 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
521 Latex_Render_Result Latex_Render(const uint8_t *source, size_t source_size) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
522 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
523 if (!source || source_size == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
524 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
525 LATEX_RENDER_INVALID_INPUT, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
526 "LaTeX source is required."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
527 if (source_size > LATEX_SOURCE_MAX_BYTES) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
528 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
529 LATEX_RENDER_INVALID_INPUT, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
530 "LaTeX source exceeds the 64 KiB limit."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
531 if (memchr(source, '\0', source_size)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
532 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
533 LATEX_RENDER_INVALID_INPUT, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
534 "LaTeX source cannot contain NUL bytes."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
535 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
536 const char *compiler = "/usr/bin/pdflatex"; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
537 if (access(compiler, X_OK) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
538 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
539 LATEX_RENDER_COMPILER_UNAVAILABLE, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
540 "The server LaTeX compiler is unavailable."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
541 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
542 char job_directory[] = "/tmp/mrjunejune-latex-XXXXXX"; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
543 if (!mkdtemp(job_directory)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
544 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
545 LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
546 "Unable to create the LaTeX workspace."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
547 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
548 char source_path[512]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
549 snprintf( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
550 source_path, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
551 sizeof(source_path), |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
552 "%s/document.tex", |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
553 job_directory); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
554 int source_fd = open( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
555 source_path, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
556 O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
557 0600); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
558 if (source_fd < 0 || !write_all(source_fd, source, source_size)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
559 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
560 if (source_fd >= 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
561 close(source_fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
562 remove_tree(job_directory, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
563 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
564 LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
565 "Unable to write the LaTeX source."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
566 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
567 close(source_fd); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
568 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
569 int output_pipe[2]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
570 if (pipe2(output_pipe, O_CLOEXEC) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
571 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
572 remove_tree(job_directory, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
573 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
574 LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
575 "Unable to capture compiler output."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
576 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
577 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
578 pid_t child = fork(); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
579 if (child < 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
580 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
581 close(output_pipe[0]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
582 close(output_pipe[1]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
583 remove_tree(job_directory, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
584 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
585 LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
586 "Unable to start the LaTeX compiler."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
587 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
588 if (child == 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
589 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
590 close(output_pipe[0]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
591 run_compiler_child(compiler, job_directory, output_pipe[1]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
592 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
593 setpgid(child, child); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
594 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
595 close(output_pipe[1]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
596 char *diagnostics = calloc(1, LATEX_DIAGNOSTICS_MAX_BYTES + 1); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
597 if (!diagnostics) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
598 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
599 kill(-child, SIGKILL); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
600 waitpid(child, NULL, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
601 close(output_pipe[0]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
602 remove_tree(job_directory, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
603 return result_with_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
604 LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
605 "Unable to allocate compiler diagnostics."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
606 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
607 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
608 bool timed_out = false; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
609 int child_status = wait_for_compiler( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
610 child, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
611 output_pipe[0], |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
612 diagnostics, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
613 LATEX_DIAGNOSTICS_MAX_BYTES + 1, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
614 &timed_out); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
615 close(output_pipe[0]); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
616 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
617 Latex_Render_Result result = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
618 .status = LATEX_RENDER_INTERNAL_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
619 .diagnostics = diagnostics, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
620 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
621 if (timed_out) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
622 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
623 result.status = LATEX_RENDER_TIMEOUT; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
624 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
625 result.diagnostics = duplicate_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
626 "Compilation exceeded the 8 second limit."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
627 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
628 else if (WIFEXITED(child_status) && WEXITSTATUS(child_status) == 127) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
629 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
630 result.status = LATEX_RENDER_COMPILER_UNAVAILABLE; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
631 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
632 else if (WIFEXITED(child_status) && WEXITSTATUS(child_status) == 123) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
633 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
634 result.status = LATEX_RENDER_SANDBOX_ERROR; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
635 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
636 result.diagnostics = duplicate_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
637 "The server could not apply the LaTeX security sandbox."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
638 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
639 else if (WIFSIGNALED(child_status) && |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
640 (WTERMSIG(child_status) == SIGXCPU || |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
641 WTERMSIG(child_status) == SIGKILL)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
642 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
643 result.status = LATEX_RENDER_TIMEOUT; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
644 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
645 result.diagnostics = duplicate_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
646 "Compilation exceeded its CPU or memory limit."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
647 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
648 else if (!WIFEXITED(child_status) || WEXITSTATUS(child_status) != 0) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
649 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
650 result.status = LATEX_RENDER_COMPILE_ERROR; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
651 if (!result.diagnostics[0]) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
652 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
653 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
654 result.diagnostics = duplicate_message("LaTeX compilation failed."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
655 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
656 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
657 else |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
658 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
659 char pdf_path[512]; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
660 snprintf(pdf_path, sizeof(pdf_path), "%s/document.pdf", job_directory); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
661 if (read_pdf(pdf_path, &result.pdf_data, &result.pdf_size)) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
662 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
663 result.status = LATEX_RENDER_OK; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
664 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
665 result.diagnostics = NULL; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
666 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
667 else |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
668 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
669 result.status = LATEX_RENDER_INTERNAL_ERROR; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
670 free(result.diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
671 result.diagnostics = duplicate_message( |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
672 "The compiler did not produce a valid PDF."); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
673 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
674 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
675 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
676 remove_tree(job_directory, 0); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
677 return result; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
678 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
679 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
680 void Latex_Render_Result_Destroy(Latex_Render_Result *result) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
681 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
682 if (!result) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
683 return; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
684 free(result->pdf_data); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
685 free(result->diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
686 memset(result, 0, sizeof(*result)); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
687 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
688 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
689 #else |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
690 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
691 #include <stdlib.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
692 #include <string.h> |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
693 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
694 Latex_Render_Result Latex_Render(const uint8_t *source, size_t source_size) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
695 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
696 (void)source; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
697 (void)source_size; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
698 const char *message = |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
699 "The LaTeX security sandbox is only available on Linux."; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
700 Latex_Render_Result result = { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
701 .status = LATEX_RENDER_SANDBOX_ERROR, |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
702 .diagnostics = malloc(strlen(message) + 1), |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
703 }; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
704 if (result.diagnostics) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
705 strcpy(result.diagnostics, message); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
706 return result; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
707 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
708 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
709 void Latex_Render_Result_Destroy(Latex_Render_Result *result) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
710 { |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
711 if (!result) |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
712 return; |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
713 free(result->pdf_data); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
714 free(result->diagnostics); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
715 memset(result, 0, sizeof(*result)); |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
716 } |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
717 |
|
b8aa08503378
[tools] Add sandboxed online LaTeX editor
MrJuneJune <me@mrjunejune.com>
parents:
diff
changeset
|
718 #endif |