This repository was archived by the owner on Jul 4, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrtld_linux.c
More file actions
1072 lines (907 loc) · 35.4 KB
/
Copy pathrtld_linux.c
File metadata and controls
1072 lines (907 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include "rtld.h"
#include <dlfcn.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
typedef int (*xstat_fn)(int, const char *, struct stat *);
typedef int (*access_fn)(const char *, int);
typedef int (*fxstat_fn)(int, int, struct stat *);
typedef int (*fxstatat_fn)(int, int, const char *, struct stat *, int);
typedef int (*newfstatat_fn)(int, const char *, struct stat *, int);
typedef int (*close_fn)(int);
typedef int (*creat_fn)(const char *, mode_t);
typedef int (*fstat_fn)(int, struct stat *);
typedef int (*chdir_fn)(const char *);
typedef int (*execve_fn)(const char *, char *const[], char *const[]);
typedef int (*link_fn)(const char *, const char *);
typedef int (*linkat_fn)(int, const char *, int, const char *, int);
typedef off_t (*lseek_fn)(int, off_t, int);
typedef int (*mkdir_fn)(const char *, mode_t);
typedef int (*mkdirat_fn)(int, const char *, mode_t);
typedef int (*open_fn)(const char *, int, ...);
typedef int (*openat_fn)(int, const char *, int, ...);
typedef int (*openat2_fn)(int, const char *, const struct open_how *, size_t);
typedef int (*faccessat_fn)(int, const char *, int, int);
typedef int (*fchmodat_fn)(int, const char *, mode_t, int);
typedef ssize_t (*pread_fn)(int, void *, size_t, off_t);
typedef ssize_t (*pwrite_fn)(int, const void *, size_t, off_t);
typedef ssize_t (*read_fn)(int, void *, size_t);
typedef ssize_t (*readlink_fn)(const char *, char *, size_t);
typedef ssize_t (*readlinkat_fn)(int, const char *, char *, size_t);
typedef int (*rename_fn)(const char *, const char *);
typedef int (*renameat_fn)(int, const char *, int, const char *);
typedef int (*renameat2_fn)(int, const char *, int, const char *, unsigned int);
typedef int (*rmdir_fn)(const char *);
typedef char *(*getcwd_fn)(char *, size_t);
typedef int (*stat_fn)(const char *, struct stat *);
typedef int (*statx_fn)(int, const char *, int, unsigned int, struct statx *);
typedef int (*symlink_fn)(const char *, const char *);
typedef int (*symlinkat_fn)(const char *, int, const char *);
typedef int (*truncate_fn)(const char *, off_t);
typedef int (*unlink_fn)(const char *);
typedef int (*unlinkat_fn)(int, const char *, int);
typedef int (*utimensat_fn)(int, const char *, const struct timespec[2], int);
typedef int (*statfs_fn)(const char *, struct statvfs *);
typedef ssize_t (*write_fn)(int, const void *, size_t);
typedef int (*euidaccess_fn)(const char *, int);
static pthread_once_t resolve_once = PTHREAD_ONCE_INIT;
static pthread_once_t rewrite_once = PTHREAD_ONCE_INIT;
static fxstat_fn real___fxstat;
static fxstatat_fn real___fxstatat;
static newfstatat_fn real_newfstatat;
static xstat_fn real___lxstat;
static xstat_fn real___xstat;
static access_fn real_access;
static chdir_fn real_chdir;
static execve_fn real_execve;
static close_fn real_close;
static creat_fn real_creat;
static fstat_fn real_fstat;
static faccessat_fn real_faccessat;
static fchmodat_fn real_fchmodat;
static link_fn real_link;
static linkat_fn real_linkat;
static lseek_fn real_lseek;
static mkdir_fn real_mkdir;
static mkdirat_fn real_mkdirat;
static open_fn real_open;
static openat_fn real_openat;
static openat2_fn real_openat2;
static pread_fn real_pread;
static pwrite_fn real_pwrite;
static getcwd_fn real_getcwd;
static read_fn real_read;
static readlink_fn real_readlink;
static readlinkat_fn real_readlinkat;
static rename_fn real_rename;
static renameat_fn real_renameat;
static renameat2_fn real_renameat2;
static rmdir_fn real_rmdir;
static stat_fn real_lstat;
static stat_fn real_stat;
static statx_fn real_statx;
static statfs_fn real_statfs;
static symlink_fn real_symlink;
static symlinkat_fn real_symlinkat;
static truncate_fn real_truncate;
static unlink_fn real_unlink;
static unlinkat_fn real_unlinkat;
static utimensat_fn real_utimensat;
static euidaccess_fn real_euidaccess;
static write_fn real_write;
struct rewrite_policy {
int enabled;
size_t from_len;
size_t to_len;
char from[PATH_MAX];
char to[PATH_MAX];
};
static struct rewrite_policy rewrite_policy;
static void resolve_symbols(void) {
real___fxstat = (fxstat_fn)dlsym(RTLD_NEXT, "__fxstat");
real___fxstatat = (fxstatat_fn)dlsym(RTLD_NEXT, "__fxstatat");
real_newfstatat = (newfstatat_fn)dlsym(RTLD_NEXT, "newfstatat");
real___lxstat = (xstat_fn)dlsym(RTLD_NEXT, "__lxstat");
real___xstat = (xstat_fn)dlsym(RTLD_NEXT, "__xstat");
real_access = (access_fn)dlsym(RTLD_NEXT, "access");
real_chdir = (chdir_fn)dlsym(RTLD_NEXT, "chdir");
real_execve = (execve_fn)dlsym(RTLD_NEXT, "execve");
real_close = (close_fn)dlsym(RTLD_NEXT, "close");
real_creat = (creat_fn)dlsym(RTLD_NEXT, "creat");
real_fstat = (fstat_fn)dlsym(RTLD_NEXT, "fstat");
real_faccessat = (faccessat_fn)dlsym(RTLD_NEXT, "faccessat");
real_fchmodat = (fchmodat_fn)dlsym(RTLD_NEXT, "fchmodat");
real_link = (link_fn)dlsym(RTLD_NEXT, "link");
real_linkat = (linkat_fn)dlsym(RTLD_NEXT, "linkat");
real_lseek = (lseek_fn)dlsym(RTLD_NEXT, "lseek");
real_mkdir = (mkdir_fn)dlsym(RTLD_NEXT, "mkdir");
real_mkdirat = (mkdirat_fn)dlsym(RTLD_NEXT, "mkdirat");
real_open = (open_fn)dlsym(RTLD_NEXT, "open");
real_openat = (openat_fn)dlsym(RTLD_NEXT, "openat");
real_openat2 = (openat2_fn)dlsym(RTLD_NEXT, "openat2");
real_pread = (pread_fn)dlsym(RTLD_NEXT, "pread");
real_pwrite = (pwrite_fn)dlsym(RTLD_NEXT, "pwrite");
real_getcwd = (getcwd_fn)dlsym(RTLD_NEXT, "getcwd");
real_read = (read_fn)dlsym(RTLD_NEXT, "read");
real_readlink = (readlink_fn)dlsym(RTLD_NEXT, "readlink");
real_readlinkat = (readlinkat_fn)dlsym(RTLD_NEXT, "readlinkat");
real_rename = (rename_fn)dlsym(RTLD_NEXT, "rename");
real_renameat = (renameat_fn)dlsym(RTLD_NEXT, "renameat");
real_renameat2 = (renameat2_fn)dlsym(RTLD_NEXT, "renameat2");
real_rmdir = (rmdir_fn)dlsym(RTLD_NEXT, "rmdir");
real_lstat = (stat_fn)dlsym(RTLD_NEXT, "lstat");
real_stat = (stat_fn)dlsym(RTLD_NEXT, "stat");
real_statx = (statx_fn)dlsym(RTLD_NEXT, "statx");
real_statfs = (statfs_fn)dlsym(RTLD_NEXT, "statfs");
real_symlink = (symlink_fn)dlsym(RTLD_NEXT, "symlink");
real_symlinkat = (symlinkat_fn)dlsym(RTLD_NEXT, "symlinkat");
real_truncate = (truncate_fn)dlsym(RTLD_NEXT, "truncate");
real_unlink = (unlink_fn)dlsym(RTLD_NEXT, "unlink");
real_unlinkat = (unlinkat_fn)dlsym(RTLD_NEXT, "unlinkat");
real_utimensat = (utimensat_fn)dlsym(RTLD_NEXT, "utimensat");
real_euidaccess = (euidaccess_fn)dlsym(RTLD_NEXT, "euidaccess");
real_write = (write_fn)dlsym(RTLD_NEXT, "write");
}
static void ensure_symbols(void) { pthread_once(&resolve_once, resolve_symbols); }
static int clean_path_into(const char *src, char *dst, size_t dst_len) {
size_t seg_starts[PATH_MAX];
size_t seg_count = 0;
size_t out = 0;
int absolute;
if (src == NULL || dst == NULL || dst_len == 0) {
return -1;
}
absolute = src[0] == '/';
if (absolute) {
if (dst_len < 2) {
return -1;
}
dst[out++] = '/';
}
while (*src != '\0') {
const char *start;
size_t seg_len;
while (*src == '/') {
src++;
}
if (*src == '\0') {
break;
}
start = src;
while (*src != '\0' && *src != '/') {
src++;
}
seg_len = (size_t)(src - start);
if (seg_len == 1 && start[0] == '.') {
continue;
}
if (seg_len == 2 && start[0] == '.' && start[1] == '.') {
if (seg_count > 0) {
out = seg_starts[--seg_count];
if (out == 0 && absolute) {
dst[out++] = '/';
}
continue;
}
if (absolute) {
continue;
}
}
if (seg_count >= PATH_MAX) {
return -1;
}
if (out != 0 && dst[out - 1] != '/') {
if (out + 1 >= dst_len) {
return -1;
}
dst[out++] = '/';
}
seg_starts[seg_count++] = out;
if (out + seg_len >= dst_len) {
return -1;
}
memcpy(dst + out, start, seg_len);
out += seg_len;
}
if (out == 0) {
if (dst_len < 2) {
return -1;
}
if (absolute) {
dst[out++] = '/';
} else {
dst[out++] = '.';
}
}
dst[out] = '\0';
return 0;
}
static int join_and_clean_path(const char *base, const char *path, char *dst, size_t dst_len) {
char joined[PATH_MAX];
if (base == NULL || path == NULL || dst == NULL) {
return -1;
}
if (snprintf(joined, sizeof(joined), "%s/%s", base, path) >= (int)sizeof(joined)) {
return -1;
}
return clean_path_into(joined, dst, dst_len);
}
static int path_matches_prefix(const char *path, const char *prefix, size_t prefix_len) {
if (strncmp(path, prefix, prefix_len) != 0) {
return 0;
}
return path[prefix_len] == '\0' || path[prefix_len] == '/';
}
static int substitute_prefix(const char *path, char *dst, size_t dst_len) {
size_t suffix_len;
if (!rewrite_policy.enabled ||
!path_matches_prefix(path, rewrite_policy.from, rewrite_policy.from_len)) {
return -1;
}
suffix_len = strlen(path + rewrite_policy.from_len);
if (rewrite_policy.to_len + suffix_len + 1 > dst_len) {
return -1;
}
memcpy(dst, rewrite_policy.to, rewrite_policy.to_len);
memcpy(dst + rewrite_policy.to_len, path + rewrite_policy.from_len, suffix_len + 1);
return 0;
}
static int substitute_prefix_reverse(const char *path, char *dst, size_t dst_len) {
size_t suffix_len;
if (!rewrite_policy.enabled ||
!path_matches_prefix(path, rewrite_policy.to, rewrite_policy.to_len)) {
return -1;
}
suffix_len = strlen(path + rewrite_policy.to_len);
if (rewrite_policy.from_len + suffix_len + 1 > dst_len) {
return -1;
}
memcpy(dst, rewrite_policy.from, rewrite_policy.from_len);
memcpy(dst + rewrite_policy.from_len, path + rewrite_policy.to_len, suffix_len + 1);
return 0;
}
static void init_rewrite_policy(void) {
const char *from_env;
const char *to_env;
char cleaned_from[PATH_MAX];
char cleaned_to[PATH_MAX];
rewrite_policy.enabled = 0;
rewrite_policy.from[0] = '\0';
rewrite_policy.to[0] = '\0';
rewrite_policy.from_len = 0;
rewrite_policy.to_len = 0;
from_env = getenv("LD_PRELOAD_REWRITE_FROM");
to_env = getenv("LD_PRELOAD_REWRITE_TO");
if (from_env == NULL || to_env == NULL || from_env[0] == '\0' || to_env[0] == '\0') {
return;
}
if (from_env[0] != '/' || to_env[0] != '/') {
return;
}
if (clean_path_into(from_env, cleaned_from, sizeof(cleaned_from)) != 0 ||
clean_path_into(to_env, cleaned_to, sizeof(cleaned_to)) != 0) {
return;
}
if (cleaned_from[0] != '/' || cleaned_to[0] != '/') {
return;
}
memcpy(rewrite_policy.from, cleaned_from, sizeof(cleaned_from));
memcpy(rewrite_policy.to, cleaned_to, sizeof(cleaned_to));
rewrite_policy.from_len = strlen(rewrite_policy.from);
rewrite_policy.to_len = strlen(rewrite_policy.to);
rewrite_policy.enabled = 1;
}
static const struct rewrite_policy *get_rewrite_policy(void) {
pthread_once(&rewrite_once, init_rewrite_policy);
return &rewrite_policy;
}
static int rewrite_absolute_path(const char *pathname, char *dst, size_t dst_len) {
char cleaned[PATH_MAX];
if (pathname == NULL || pathname[0] != '/') {
return 0;
}
if (!get_rewrite_policy()->enabled) {
return 0;
}
if (clean_path_into(pathname, cleaned, sizeof(cleaned)) != 0) {
return 0;
}
if (substitute_prefix(cleaned, dst, dst_len) != 0) {
return 0;
}
return 1;
}
static int rewrite_relative_path_from_dirfd(int dirfd, const char *pathname, char *dst,
size_t dst_len) {
char base[PATH_MAX];
char resolved[PATH_MAX];
if (pathname == NULL || pathname[0] == '/') {
return 0;
}
if (!get_rewrite_policy()->enabled) {
return 0;
}
ensure_symbols();
if (dirfd == AT_FDCWD) {
if (real_getcwd == NULL) {
return 0;
}
if (real_getcwd(base, sizeof(base)) == NULL) {
return 0;
}
} else {
char fd_path[64];
ssize_t n;
if (snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", dirfd) >= (int)sizeof(fd_path)) {
return 0;
}
ensure_symbols();
if (real_readlink == NULL) {
return 0;
}
n = real_readlink(fd_path, base, sizeof(base) - 1);
if (n < 0 || n >= (ssize_t)sizeof(base) - 1) {
return 0;
}
base[n] = '\0';
if (base[0] != '/') {
return 0;
}
if (clean_path_into(base, resolved, sizeof(resolved)) != 0) {
return 0;
}
memcpy(base, resolved, sizeof(base));
}
if (join_and_clean_path(base, pathname, resolved, sizeof(resolved)) != 0) {
return 0;
}
if (substitute_prefix(resolved, dst, dst_len) != 0) {
return 0;
}
return 1;
}
static const char *rewrite_path_argument(const char *pathname, char *buf, size_t buf_len) {
if (pathname == NULL) {
return pathname;
}
if (rewrite_absolute_path(pathname, buf, buf_len)) {
return buf;
}
return pathname;
}
static const char *rewrite_cwd_relative_argument(const char *pathname, char *buf, size_t buf_len) {
if (pathname == NULL) {
return pathname;
}
if (pathname[0] == '/') {
return rewrite_path_argument(pathname, buf, buf_len);
}
if (rewrite_relative_path_from_dirfd(AT_FDCWD, pathname, buf, buf_len)) {
return buf;
}
return pathname;
}
static const char *rewrite_at_argument(int *dirfd, const char *pathname, char *buf,
size_t buf_len) {
if (pathname == NULL) {
return pathname;
}
if (pathname[0] == '/') {
return rewrite_path_argument(pathname, buf, buf_len);
}
if (rewrite_relative_path_from_dirfd(*dirfd, pathname, buf, buf_len)) {
*dirfd = AT_FDCWD;
return buf;
}
return pathname;
}
#define REQUIRE_SYMBOL(sym) \
do { \
ensure_symbols(); \
if ((sym) == NULL) { \
errno = ENOSYS; \
return -1; \
} \
} while (0)
#define REQUIRE_SYMBOL_PTR(sym) \
do { \
ensure_symbols(); \
if ((sym) == NULL) { \
errno = ENOSYS; \
return NULL; \
} \
} while (0)
int orig___fxstat(int ver, int fd, struct stat *cstat) {
REQUIRE_SYMBOL(real___fxstat);
return real___fxstat(ver, fd, cstat);
}
int orig___fxstatat(int ver, int dirfd, const char *pathname, struct stat *cstat, int flags) {
REQUIRE_SYMBOL(real___fxstatat);
return real___fxstatat(ver, dirfd, pathname, cstat, flags);
}
int orig___lxstat(int ver, const char *pathname, struct stat *cstat) {
REQUIRE_SYMBOL(real___lxstat);
return real___lxstat(ver, pathname, cstat);
}
int orig___xstat(int ver, const char *pathname, struct stat *cstat) {
REQUIRE_SYMBOL(real___xstat);
return real___xstat(ver, pathname, cstat);
}
int orig_access(const char *pathname, int mode) {
REQUIRE_SYMBOL(real_access);
return real_access(pathname, mode);
}
int orig_chdir(const char *path) {
REQUIRE_SYMBOL(real_chdir);
return real_chdir(path);
}
int orig_execve(const char *pathname, char *const argv[], char *const envp[]) {
REQUIRE_SYMBOL(real_execve);
return real_execve(pathname, argv, envp);
}
int orig_close(int fd) {
REQUIRE_SYMBOL(real_close);
return real_close(fd);
}
int orig_creat(const char *pathname, mode_t mode) {
REQUIRE_SYMBOL(real_creat);
return real_creat(pathname, mode);
}
int orig_fstat(int fd, struct stat *cstat) {
REQUIRE_SYMBOL(real_fstat);
return real_fstat(fd, cstat);
}
int orig_newfstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) {
REQUIRE_SYMBOL(real_newfstatat);
return real_newfstatat(dirfd, pathname, statbuf, flags);
}
int orig_faccessat(int dirfd, const char *pathname, int mode, int flags) {
REQUIRE_SYMBOL(real_faccessat);
return real_faccessat(dirfd, pathname, mode, flags);
}
int orig_fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
REQUIRE_SYMBOL(real_fchmodat);
return real_fchmodat(dirfd, pathname, mode, flags);
}
int orig_link(const char *oldpath, const char *newpath) {
REQUIRE_SYMBOL(real_link);
return real_link(oldpath, newpath);
}
int orig_linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
REQUIRE_SYMBOL(real_linkat);
return real_linkat(olddirfd, oldpath, newdirfd, newpath, flags);
}
off_t orig_lseek(int fd, off_t offset, int whence) {
REQUIRE_SYMBOL(real_lseek);
return real_lseek(fd, offset, whence);
}
int orig_mkdir(const char *pathname, mode_t mode) {
REQUIRE_SYMBOL(real_mkdir);
return real_mkdir(pathname, mode);
}
int orig_mkdirat(int dirfd, const char *pathname, mode_t mode) {
REQUIRE_SYMBOL(real_mkdirat);
return real_mkdirat(dirfd, pathname, mode);
}
int orig_open(const char *pathname, int flags, mode_t mode) {
REQUIRE_SYMBOL(real_open);
return real_open(pathname, flags, mode);
}
int orig_openat(int dirfd, const char *pathname, int flags, mode_t mode) {
REQUIRE_SYMBOL(real_openat);
return real_openat(dirfd, pathname, flags, mode);
}
int orig_openat2(int dirfd, const char *pathname, const struct open_how *how, size_t size) {
REQUIRE_SYMBOL(real_openat2);
return real_openat2(dirfd, pathname, how, size);
}
char *orig_getcwd(char *buf, size_t size) {
REQUIRE_SYMBOL_PTR(real_getcwd);
return real_getcwd(buf, size);
}
int orig_euidaccess(const char *pathname, int mode) {
REQUIRE_SYMBOL(real_euidaccess);
return real_euidaccess(pathname, mode);
}
ssize_t orig_pread(int fd, void *buf, size_t count, off_t offset) {
REQUIRE_SYMBOL(real_pread);
return real_pread(fd, buf, count, offset);
}
ssize_t orig_pwrite(int fd, const void *buf, size_t count, off_t offset) {
REQUIRE_SYMBOL(real_pwrite);
return real_pwrite(fd, buf, count, offset);
}
ssize_t orig_read(int fd, void *buf, size_t count) {
REQUIRE_SYMBOL(real_read);
return real_read(fd, buf, count);
}
ssize_t orig_readlink(const char *pathname, void *buf, size_t bufsiz) {
REQUIRE_SYMBOL(real_readlink);
return real_readlink(pathname, (char *)buf, bufsiz);
}
ssize_t orig_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
REQUIRE_SYMBOL(real_readlinkat);
return real_readlinkat(dirfd, pathname, buf, bufsiz);
}
int orig_rename(const char *oldpath, const char *newpath) {
REQUIRE_SYMBOL(real_rename);
return real_rename(oldpath, newpath);
}
int orig_renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
REQUIRE_SYMBOL(real_renameat);
return real_renameat(olddirfd, oldpath, newdirfd, newpath);
}
int orig_renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath,
unsigned int flags) {
REQUIRE_SYMBOL(real_renameat2);
return real_renameat2(olddirfd, oldpath, newdirfd, newpath, flags);
}
int orig_rmdir(const char *pathname) {
REQUIRE_SYMBOL(real_rmdir);
return real_rmdir(pathname);
}
int orig_lstat(const char *pathname, struct stat *cstat) {
REQUIRE_SYMBOL(real_lstat);
return real_lstat(pathname, cstat);
}
int orig_statx(int dirfd, const char *pathname, int flags, unsigned int mask,
struct statx *statxbuf) {
REQUIRE_SYMBOL(real_statx);
return real_statx(dirfd, pathname, flags, mask, statxbuf);
}
int orig_stat(const char *pathname, struct stat *cstat) {
REQUIRE_SYMBOL(real_stat);
return real_stat(pathname, cstat);
}
int orig_statfs(const char *path, struct statvfs *buf) {
REQUIRE_SYMBOL(real_statfs);
return real_statfs(path, buf);
}
int orig_symlink(const char *target, const char *linkpath) {
REQUIRE_SYMBOL(real_symlink);
return real_symlink(target, linkpath);
}
int orig_symlinkat(const char *target, int newdirfd, const char *linkpath) {
REQUIRE_SYMBOL(real_symlinkat);
return real_symlinkat(target, newdirfd, linkpath);
}
int orig_truncate(const char *pathname, off_t length) {
REQUIRE_SYMBOL(real_truncate);
return real_truncate(pathname, length);
}
int orig_unlink(const char *pathname) {
REQUIRE_SYMBOL(real_unlink);
return real_unlink(pathname);
}
int orig_unlinkat(int dirfd, const char *pathname, int flags) {
REQUIRE_SYMBOL(real_unlinkat);
return real_unlinkat(dirfd, pathname, flags);
}
int orig_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
REQUIRE_SYMBOL(real_utimensat);
return real_utimensat(dirfd, pathname, times, flags);
}
ssize_t orig_write(int fd, const void *buf, size_t count) {
REQUIRE_SYMBOL(real_write);
return real_write(fd, buf, count);
}
int __fxstat(int ver, int fd, struct stat *cstat) { return orig___fxstat(ver, fd, cstat); }
int __fxstatat(int ver, int dirfd, const char *pathname, struct stat *cstat, int flags) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig___fxstatat(ver, call_dirfd, target, cstat, flags);
}
int newfstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_newfstatat(call_dirfd, target, statbuf, flags);
}
int __lxstat(int ver, const char *pathname, struct stat *cstat) {
char rewritten[PATH_MAX];
return orig___lxstat(ver, rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), cstat);
}
int __xstat(int ver, const char *pathname, struct stat *cstat) {
char rewritten[PATH_MAX];
return orig___xstat(ver, rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), cstat);
}
int access(const char *pathname, int mode) {
char rewritten[PATH_MAX];
return orig_access(rewrite_cwd_relative_argument(pathname, rewritten, sizeof(rewritten)), mode);
}
int chdir(const char *path) {
char rewritten[PATH_MAX];
return orig_chdir(rewrite_cwd_relative_argument(path, rewritten, sizeof(rewritten)));
}
int close(int fd) { return orig_close(fd); }
int creat(const char *pathname, mode_t mode) {
char rewritten[PATH_MAX];
return orig_creat(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), mode);
}
int execve(const char *pathname, char *const argv[], char *const envp[]) {
char rewritten[PATH_MAX];
return orig_execve(rewrite_cwd_relative_argument(pathname, rewritten, sizeof(rewritten)), argv,
envp);
}
int euidaccess(const char *pathname, int mode) {
char rewritten[PATH_MAX];
return orig_euidaccess(rewrite_cwd_relative_argument(pathname, rewritten, sizeof(rewritten)),
mode);
}
int fstat(int fd, struct stat *cstat) { return orig_fstat(fd, cstat); }
int faccessat(int dirfd, const char *pathname, int mode, int flags) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_faccessat(call_dirfd, target, mode, flags);
}
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_fchmodat(call_dirfd, target, mode, flags);
}
int link(const char *oldpath, const char *newpath) {
char old_rewritten[PATH_MAX];
char new_rewritten[PATH_MAX];
return orig_link(rewrite_cwd_relative_argument(oldpath, old_rewritten, sizeof(old_rewritten)),
rewrite_cwd_relative_argument(newpath, new_rewritten, sizeof(new_rewritten)));
}
int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) {
char old_rewritten[PATH_MAX];
char new_rewritten[PATH_MAX];
const char *effective_oldpath =
rewrite_at_argument(&olddirfd, oldpath, old_rewritten, sizeof(old_rewritten));
const char *effective_newpath =
rewrite_at_argument(&newdirfd, newpath, new_rewritten, sizeof(new_rewritten));
return orig_linkat(olddirfd, effective_oldpath, newdirfd, effective_newpath, flags);
}
off_t lseek(int fd, off_t offset, int whence) { return orig_lseek(fd, offset, whence); }
int mkdir(const char *pathname, mode_t mode) {
char rewritten[PATH_MAX];
return orig_mkdir(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), mode);
}
int mkdirat(int dirfd, const char *pathname, mode_t mode) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_mkdirat(call_dirfd, target, mode);
}
int open(const char *pathname, int flags, ...) {
mode_t mode = 0;
if (flags & (O_CREAT | O_TMPFILE)) {
va_list args;
va_start(args, flags);
mode = (mode_t)va_arg(args, int);
va_end(args);
}
{
char rewritten[PATH_MAX];
return orig_open(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), flags,
mode);
}
}
int openat(int dirfd, const char *pathname, int flags, ...) {
mode_t mode = 0;
if (flags & (O_CREAT | O_TMPFILE)) {
va_list args;
va_start(args, flags);
mode = (mode_t)va_arg(args, int);
va_end(args);
}
{
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten,
sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_openat(call_dirfd, target, flags, mode);
}
}
int openat2(int dirfd, const char *pathname, const struct open_how *how, size_t size) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_openat2(call_dirfd, target, how, size);
}
char *getcwd(char *buf, size_t size) {
char rewritten[PATH_MAX];
char *result;
result = orig_getcwd(buf, size);
if (result == NULL) {
return NULL;
}
if (!get_rewrite_policy()->enabled) {
return result;
}
if (substitute_prefix_reverse(buf, rewritten, size) != 0) {
return result;
}
if (strlen(rewritten) + 1 > size) {
return result;
}
memcpy(buf, rewritten, strlen(rewritten) + 1);
return buf;
}
ssize_t pread(int fd, void *buf, size_t count, off_t offset) {
return orig_pread(fd, buf, count, offset);
}
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) {
return orig_pwrite(fd, buf, count, offset);
}
ssize_t read(int fd, void *buf, size_t count) { return orig_read(fd, buf, count); }
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz) {
char rewritten[PATH_MAX];
return orig_readlink(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), buf,
bufsiz);
}
ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_readlinkat(call_dirfd, target, buf, bufsiz);
}
int rename(const char *oldpath, const char *newpath) {
char old_rewritten[PATH_MAX];
char new_rewritten[PATH_MAX];
return orig_rename(
rewrite_cwd_relative_argument(oldpath, old_rewritten, sizeof(old_rewritten)),
rewrite_cwd_relative_argument(newpath, new_rewritten, sizeof(new_rewritten)));
}
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
char old_rewritten[PATH_MAX];
char new_rewritten[PATH_MAX];
const char *effective_oldpath =
rewrite_at_argument(&olddirfd, oldpath, old_rewritten, sizeof(old_rewritten));
const char *effective_newpath =
rewrite_at_argument(&newdirfd, newpath, new_rewritten, sizeof(new_rewritten));
return orig_renameat(olddirfd, effective_oldpath, newdirfd, effective_newpath);
}
int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath,
unsigned int flags) {
char old_rewritten[PATH_MAX];
char new_rewritten[PATH_MAX];
const char *effective_oldpath =
rewrite_at_argument(&olddirfd, oldpath, old_rewritten, sizeof(old_rewritten));
const char *effective_newpath =
rewrite_at_argument(&newdirfd, newpath, new_rewritten, sizeof(new_rewritten));
return orig_renameat2(olddirfd, effective_oldpath, newdirfd, effective_newpath, flags);
}
int lstat(const char *pathname, struct stat *cstat) {
char rewritten[PATH_MAX];
return orig_lstat(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), cstat);
}
int rmdir(const char *pathname) {
char rewritten[PATH_MAX];
return orig_rmdir(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)));
}
int stat(const char *pathname, struct stat *cstat) {
char rewritten[PATH_MAX];
return orig_stat(rewrite_path_argument(pathname, rewritten, sizeof(rewritten)), cstat);
}
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) {
char rewritten[PATH_MAX];
const char *target = pathname;
int call_dirfd = dirfd;
if (pathname != NULL && pathname[0] == '/') {
target = rewrite_path_argument(pathname, rewritten, sizeof(rewritten));
} else if (rewrite_relative_path_from_dirfd(dirfd, pathname, rewritten, sizeof(rewritten))) {
target = rewritten;
call_dirfd = AT_FDCWD;
}
return orig_statx(call_dirfd, target, flags, mask, statxbuf);
}
int statfs(const char *path, struct statvfs *buf) {
char rewritten[PATH_MAX];