1111#include <unistd.h>
1212
1313#include "list_util.h"
14- #include "util.h"
1514
1615#ifdef COLOR_ENFORCING
1716#define PINK "\033[38;5;198m"
@@ -44,39 +43,36 @@ void flag2letter(struct path_access *r, char *buff, size_t buff_len)
4443 return ;
4544 }
4645 memset (buff , 0 , buff_len );
47- if (r -> stat )
48- strncat (buff , "S " , buff_len );
46+ if (r -> metadata )
47+ strncat (buff , "M " , buff_len );
4948 if (r -> create )
5049 strncat (buff , "C" , buff_len );
5150 if (r -> delete )
5251 strncat (buff , "D" , buff_len );
53- if (r -> read && r -> write )
54- strncat (buff , "+" , buff_len );
55- else if (r -> read )
52+ if (r -> read )
5653 strncat (buff , "R" , buff_len );
57- else if (r -> write )
54+ if (r -> write )
5855 strncat (buff , "W" , buff_len );
5956 if (r -> list )
6057 strncat (buff , "L" , buff_len );
58+ if (r -> error )
59+ strncat (buff , "E" , buff_len );
6160}
6261
6362// TODO: The bit flags need to be changed to a typedef so we can
6463// change the bit count whenever
6564uint8_t letter2bitflag (char x )
6665{
6766 switch (x ) {
68- case 'S ' :
69- return STAT_ACCESS ;
67+ case 'M ' :
68+ return METADATA_ACCESS ;
7069 break ;
7170 case 'R' :
7271 return READ_ACCESS ;
7372 break ;
7473 case 'W' :
7574 return WRITE_ACCESS ;
7675 break ;
77- case '+' :
78- return READ_ACCESS | WRITE_ACCESS ;
79- break ;
8076 case 'C' :
8177 return CREATE_ACCESS ;
8278 break ;
@@ -86,6 +82,9 @@ uint8_t letter2bitflag(char x)
8682 case 'L' :
8783 return LIST_ACCESS ;
8884 break ;
85+ case 'E' :
86+ return ERROR_ACCESS ;
87+ break ;
8988 default :
9089 return UNKOWN_ACCESS ;
9190 break ;
@@ -365,11 +364,22 @@ bool enforce(const char *pathname,
365364 a_perm );
366365 PRINT_RESET_TERM_C ();
367366 return true;
368- } else if (a -> stat && (keys & STAT_ACCESS )) {
367+ } else if (a -> metadata && (keys & METADATA_ACCESS )) {
369368 flag2letter (a , a_perm , a_perm_len );
370369 PRINT_GREEN ();
371370 fprintf (stderr ,
372- "[ALLOWED STAT]: "
371+ "[ALLOWED METADATA OPERATION]: "
372+ "Path [%s] with permission [%s] is not in violation of the "
373+ "contract.\n" ,
374+ a -> pathname ,
375+ a_perm );
376+ PRINT_RESET_TERM_C ();
377+ return true;
378+ } else if (a -> list && (keys & LIST_ACCESS )) {
379+ flag2letter (a , a_perm , a_perm_len );
380+ PRINT_GREEN ();
381+ fprintf (stderr ,
382+ "[ALLOWED GETDENTS]: "
373383 "Path [%s] with permission [%s] is not in violation of the "
374384 "contract.\n" ,
375385 a -> pathname ,
@@ -428,16 +438,9 @@ int open(const char *pathname,
428438
429439 path_perm |= CREATE_ACCESS ;
430440 }
441+ path_perm |= METADATA_ACCESS ;
431442
432443 // SECTION: Enforce
433- if ((flags & O_RDONLY ) == O_RDONLY ) // O_RDONLY flag is 0 under the hood
434- {
435- path_perm |= READ_ACCESS ;
436- } else if (flags & O_WRONLY ) {
437- path_perm |= WRITE_ACCESS ;
438- } else if (flags & O_RDWR ) {
439- path_perm |= READ_ACCESS | WRITE_ACCESS ;
440- }
441444 if (enforce (full_path , path_perm ) != true) {
442445 return -1 ;
443446 }
@@ -517,33 +520,8 @@ fopen(const char *restrict pathname,
517520 // SECTION: Enforcing
518521 uint8_t perm_val = 0x0 ;
519522 char mode_len = strlen (mode );
520- // We need to get the values out of the flags
521- if (strcmp (mode , "r" ) == 0 ) {
522- perm_val |= READ_ACCESS ;
523- } else if (strcmp (mode , "w" ) == 0 ) {
524- perm_val |= WRITE_ACCESS ;
525- } else if (strcmp (mode , "a" ) == 0 ) {
526- perm_val |= WRITE_ACCESS ;
527- } else if (mode_len > 1 ) {
528- if (mode [1 ] == '+' ) {
529- perm_val |= READ_ACCESS | WRITE_ACCESS ;
530- // b and + are not the only things we can to an fopen mode flag call
531- // theres also e, but for now we only want to treat + like its special
532- // b and e, we dont really concern ourselves
533- } else {
534- // not a fan of this
535- switch (mode [0 ]) {
536- case 'r' :
537- perm_val |= READ_ACCESS ;
538- break ;
539- case 'w' :
540- perm_val |= WRITE_ACCESS ;
541- break ;
542- }
543- }
544- } else {
545- fprintf (stderr , "FOPEN: Unkown permission [%s]\n" , mode );
546- }
523+
524+ perm_val |= METADATA_ACCESS ;
547525
548526 char full_path [MAXPATHLEN ];
549527 if (rel2abspath (full_path , pathname , MAXPATHLEN ) == NULL ) {
@@ -581,7 +559,7 @@ int stat(const char *restrict pathname,
581559 // Couldn't convert so we fallback to pathname
582560 strncpy (full_path , pathname , MAXPATHLEN );
583561 }
584- enforce (full_path , STAT_ACCESS );
562+ enforce (full_path , METADATA_ACCESS );
585563
586564 int (* real_stat )(const char * restrict pathname , struct stat * restrict statbuf );
587565 real_stat = dlsym (RTLD_NEXT , "stat" );
@@ -622,7 +600,7 @@ int fstatat(int dirfd,
622600 // TODO: This could be made to only have 1 call to enforce instead of 2 in separate
623601 // branches
624602 if (dirfd == AT_FDCWD ) {
625- enforce (full_path , STAT_ACCESS );
603+ enforce (full_path , METADATA_ACCESS );
626604 } else {
627605 // Solve the dirfd and then glue it together with the absolute path
628606 char fd_link [MAXPATHLEN ];
@@ -638,7 +616,7 @@ int fstatat(int dirfd,
638616 } else {
639617 strcat (solved_path , pathname );
640618 }
641- enforce (solved_path , STAT_ACCESS );
619+ enforce (solved_path , METADATA_ACCESS );
642620 }
643621
644622 int (* real_fstatat )(int dirfd , const char * restrict pathname , struct stat * restrict statbuf , int flags );
@@ -669,3 +647,24 @@ int remove(const char *pathname)
669647 real_remove = dlsym (RTLD_NEXT , "remove" );
670648 return real_remove (pathname );
671649}
650+
651+ ssize_t getdents64 (int fd , void * dirp , size_t count )
652+ {
653+ char fd_link [BUFSIZ ];
654+ snprintf (fd_link , BUFSIZ , "/proc/self/fd/%d" , fd );
655+ char solved_path [BUFSIZ ];
656+ size_t solved_path_len = readlink (fd_link , solved_path , BUFSIZ );
657+ solved_path [solved_path_len ] = '\0' ;
658+
659+ PRINT_YELLOW ();
660+ fprintf (stderr , "[GETDENTS64]: Caught path: [%s]\n" , solved_path );
661+ PRINT_RESET_TERM_C ();
662+
663+ if (enforce (solved_path , LIST_ACCESS ) != true) {
664+ return -1 ;
665+ }
666+
667+ ssize_t (* real_getdents64 )(int fd , void * dirp , size_t count );
668+ real_getdents64 = dlsym (RTLD_NEXT , "getdents64" );
669+ return real_getdents64 (fd , dirp , count );
670+ }
0 commit comments