kernel+libc: Add access()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
bd8aaa917f
commit
88d1da59e8
@ -1,6 +1,7 @@
|
|||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "sys/Syscall.h"
|
#include "sys/Syscall.h"
|
||||||
#include "thread/Scheduler.h"
|
#include "thread/Scheduler.h"
|
||||||
|
#include <bits/access.h>
|
||||||
#include <bits/atfile.h>
|
#include <bits/atfile.h>
|
||||||
#include <bits/modes.h>
|
#include <bits/modes.h>
|
||||||
#include <bits/struct_stat.h>
|
#include <bits/struct_stat.h>
|
||||||
@ -49,3 +50,33 @@ Result<u64> sys_fstatat(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result<u64> sys_faccessat(Registers*, SyscallArgs args)
|
||||||
|
{
|
||||||
|
int dirfd = (int)args[0];
|
||||||
|
auto path = TRY(MemoryManager::strdup_from_user(args[1]));
|
||||||
|
int amode = (int)args[2];
|
||||||
|
int flags = (int)args[3];
|
||||||
|
|
||||||
|
Credentials creds;
|
||||||
|
|
||||||
|
auto* current = Scheduler::current();
|
||||||
|
|
||||||
|
if (flags & AT_EACCESS) creds = current->auth;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto auth = current->auth;
|
||||||
|
creds.euid = auth.uid;
|
||||||
|
creds.egid = auth.gid;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto inode = TRY(current->resolve_atfile(dirfd, path, false, true));
|
||||||
|
|
||||||
|
if ((amode & R_OK) && !VFS::can_read(inode, creds)) return err(EACCES);
|
||||||
|
if ((amode & W_OK) && !VFS::can_write(inode, creds)) return err(EACCES);
|
||||||
|
if ((amode & X_OK) && !VFS::can_execute(inode, creds)) return err(EACCES);
|
||||||
|
|
||||||
|
// Either all checks succeeded, or amode == F_OK and the file exists, since resolve_atfile() would have failed
|
||||||
|
// otherwise.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
12
libc/include/bits/access.h
Normal file
12
libc/include/bits/access.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/* bits/access.h: Definitions for the access() function. */
|
||||||
|
|
||||||
|
#ifndef _BITS_ACCESS_H
|
||||||
|
#define _BITS_ACCESS_H
|
||||||
|
|
||||||
|
#define R_OK 1
|
||||||
|
#define W_OK 2
|
||||||
|
#define X_OK 4
|
||||||
|
|
||||||
|
#define F_OK 0
|
||||||
|
|
||||||
|
#endif
|
@ -10,4 +10,6 @@
|
|||||||
|
|
||||||
#define AT_REMOVEDIR 1
|
#define AT_REMOVEDIR 1
|
||||||
|
|
||||||
|
#define AT_EACCESS 1
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#define __need_NULL
|
#define __need_NULL
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include <bits/access.h>
|
||||||
#include <bits/seek.h>
|
#include <bits/seek.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -151,6 +152,15 @@ extern "C"
|
|||||||
/* Create a hard link relative to a file descriptor. */
|
/* Create a hard link relative to a file descriptor. */
|
||||||
int linkat(int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags);
|
int linkat(int olddirfd, const char* oldpath, int newdirfd, const char* newpath, int flags);
|
||||||
|
|
||||||
|
/* Check for a file's accessibility. */
|
||||||
|
int access(const char* path, int amode);
|
||||||
|
|
||||||
|
/* Check for a file's accessibility using the effective user and group IDs. */
|
||||||
|
int eaccess(const char* path, int amode);
|
||||||
|
|
||||||
|
/* Check for a file's accessibility relative to a file descriptor. */
|
||||||
|
int faccessat(int dirfd, const char* path, int amode, int flags);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -442,4 +442,22 @@ extern "C"
|
|||||||
long rc = syscall(SYS_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
|
long rc = syscall(SYS_linkat, olddirfd, oldpath, newdirfd, newpath, flags);
|
||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int access(const char* path, int amode)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_faccessat, AT_FDCWD, path, amode, 0);
|
||||||
|
__errno_return(rc, int);
|
||||||
|
}
|
||||||
|
|
||||||
|
int eaccess(const char* path, int amode)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_faccessat, AT_FDCWD, path, amode, AT_EACCESS);
|
||||||
|
__errno_return(rc, int);
|
||||||
|
}
|
||||||
|
|
||||||
|
int faccessat(int dirfd, const char* path, int amode, int flags)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_faccessat, dirfd, path, amode, flags);
|
||||||
|
__errno_return(rc, int);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
|
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
|
||||||
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
|
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
|
||||||
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
|
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
|
||||||
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat)
|
_e(umount) _e(pstat) _e(getrusage) _e(symlinkat) _e(readlinkat) _e(umask) _e(linkat) _e(faccessat)
|
||||||
|
|
||||||
enum Syscalls
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user