commit f76c558
Devine Lu Linvega
·
2025-01-22 19:47:53 +0000 UTC
parent aba5a7e
Removed file sandbox
1 files changed,
+2,
-58
+2,
-58
1@@ -53,7 +53,6 @@ typedef struct {
2 DIR_READ,
3 DIR_WRITE
4 } state;
5- int outside_sandbox;
6 } UxnFile;
7
8 static UxnFile uxn_file[2];
9@@ -110,7 +109,6 @@ file_reset(UxnFile *c)
10 if(c->dir != NULL)
11 closedir(c->dir), c->dir = NULL;
12 c->state = IDLE;
13- c->outside_sandbox = 0;
14 }
15
16 static Uint16
17@@ -120,62 +118,13 @@ file_read_dir(UxnFile *c, Uint8 *dest, Uint16 len)
18 static char pathname[0x2000];
19 struct dirent *de = readdir(c->dir);
20 for(; de != NULL; de = readdir(c->dir)) {
21- if(de->d_name[0] == '.' && de->d_name[1] == 0)
22- continue;
23+ if(de->d_name[0] == '.' && de->d_name[1] == 0) continue;
24 snprintf(pathname, 0x2000, c->filepath[0] == '.' ? "%s/%s" : "%s%s", c->filepath, de->d_name);
25 dest += put_statfile(dest, len, pathname, de->d_name);
26 }
27 return dest - p;
28 }
29
30-static char *
31-retry_realpath(const char *file_name)
32-{
33- char *r, p[PATH_MAX] = {'\0'}, *x;
34- int fnlen;
35- if(file_name == NULL) {
36- errno = EINVAL;
37- return NULL;
38- } else if((fnlen = strlen(file_name)) >= PATH_MAX) {
39- errno = ENAMETOOLONG;
40- return NULL;
41- }
42- if(notdriveroot(file_name)) {
43- /* TODO: use a macro instead of '/' for absolute path first character so that other systems can work */
44- /* if a relative path, prepend cwd */
45- getcwd(p, sizeof(p));
46- if(strlen(p) + strlen(DIR_SEP_STR) + fnlen >= PATH_MAX) {
47- errno = ENAMETOOLONG;
48- return NULL;
49- }
50- strcat(p, DIR_SEP_STR); /* TODO: use a macro instead of '/' for the path delimiter */
51- }
52- strcat(p, file_name);
53- while((r = realpath(p, NULL)) == NULL) {
54- if(errno != ENOENT)
55- return NULL;
56- x = strrchr(p, DIR_SEP_CHAR); /* TODO: path delimiter macro */
57- if(x)
58- *x = '\0';
59- else
60- return NULL;
61- }
62- return r;
63-}
64-
65-static void
66-file_check_sandbox(UxnFile *c)
67-{
68- char *x, *rp, cwd[PATH_MAX] = {'\0'};
69- x = getcwd(cwd, sizeof(cwd));
70- rp = retry_realpath(c->filepath);
71- if(rp == NULL || (x && pathcmp(cwd, rp, strlen(cwd)) != 0)) {
72- c->outside_sandbox = 1;
73- fprintf(stderr, "file warning: blocked attempt to access %s outside of sandbox\n", c->filepath);
74- }
75- free(rp);
76-}
77-
78 static Uint16
79 file_init(UxnFile *c, Uint16 addr)
80 {
81@@ -187,7 +136,6 @@ file_init(UxnFile *c, Uint16 addr)
82 if(len > max_len) len = max_len;
83 while(len) {
84 if((*p++ = *filename++) == '\0') {
85- file_check_sandbox(c);
86 return 0;
87 }
88 len--;
89@@ -202,7 +150,6 @@ file_read(UxnFile *c, Uint16 addr, int len)
90 void *dest = &uxn.ram[addr];
91 if(len > 0x10000 - addr)
92 len = 0x10000 - addr;
93- if(c->outside_sandbox) return 0;
94 if(c->state != FILE_READ && c->state != DIR_READ) {
95 file_reset(c);
96 if((c->dir = opendir(c->filepath)) != NULL)
97@@ -256,7 +203,6 @@ file_write(UxnFile *c, Uint16 addr, Uint16 len, Uint8 flags)
98 Uint16 ret = 0;
99 if(len > 0x10000 - addr)
100 len = 0x10000 - addr;
101- if(c->outside_sandbox) return 0;
102 ensure_parent_dirs(c->filepath);
103 if(c->state != FILE_WRITE && c->state != DIR_WRITE) {
104 file_reset(c);
105@@ -280,8 +226,6 @@ file_stat(UxnFile *c, Uint16 addr, Uint16 len)
106 {
107 int err, dir;
108 struct stat st;
109- if(c->outside_sandbox)
110- return 0;
111 err = stat(c->filepath, &st);
112 dir = S_ISDIR(st.st_mode);
113 return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
114@@ -290,7 +234,7 @@ file_stat(UxnFile *c, Uint16 addr, Uint16 len)
115 static Uint16
116 file_delete(UxnFile *c)
117 {
118- return c->outside_sandbox ? 0 : unlink(c->filepath);
119+ return unlink(c->filepath);
120 }
121
122 /* file registers */