auth: filesystem unlink action check + add parent vnode to auth_filesystem_check

This commit is contained in:
Matheus
2024-08-27 21:46:33 -03:00
parent 284d91f6e7
commit ba6506b9d7
9 changed files with 55 additions and 22 deletions
+1 -1
View File
@@ -191,7 +191,7 @@ int devfs_munmap(vnode_t *node, void *addr, uintmax_t offset, int flags, cred_t
} }
int devfs_access(vnode_t *vnode, mode_t mode, cred_t *cred) { int devfs_access(vnode_t *vnode, mode_t mode, cred_t *cred) {
return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode) ? EACCES : 0; return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode, NULL) ? EACCES : 0;
} }
int devfs_isatty(vnode_t *node) { int devfs_isatty(vnode_t *node) {
+1 -1
View File
@@ -1243,7 +1243,7 @@ static int ext2_getdents(vnode_t *vnode, dent_t *buffer, size_t count, uintmax_t
} }
static int ext2_access(vnode_t *vnode, mode_t mode, cred_t *cred) { static int ext2_access(vnode_t *vnode, mode_t mode, cred_t *cred) {
return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode) ? EACCES : 0; return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode, NULL) ? EACCES : 0;
} }
static int ext2_readlink(vnode_t *vnode, char **link, cred_t *cred) { static int ext2_readlink(vnode_t *vnode, char **link, cred_t *cred) {
+1 -1
View File
@@ -238,7 +238,7 @@ static int tmpfs_write(vnode_t *node, void *buffer, size_t size, uintmax_t offse
} }
static int tmpfs_access(vnode_t *vnode, mode_t mode, cred_t *cred) { static int tmpfs_access(vnode_t *vnode, mode_t mode, cred_t *cred) {
return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode) ? EACCES : 0; return auth_filesystem_check(cred, auth_filesystem_convertaccess(mode), vnode, NULL) ? EACCES : 0;
} }
static int tmpfs_unlink(vnode_t *node, vnode_t *child, char *name, cred_t *cred) { static int tmpfs_unlink(vnode_t *node, vnode_t *child, char *name, cred_t *cred) {
+6 -14
View File
@@ -85,7 +85,7 @@ int vfs_mount(vnode_t *backing, vnode_t *pathref, char *path, char *name, void *
if (err) if (err)
return err; return err;
err = auth_filesystem_check(getcred(), AUTH_ACTIONS_FILESYSTEM_MOUNT, mounton); err = auth_filesystem_check(getcred(), AUTH_ACTIONS_FILESYSTEM_MOUNT, mounton, NULL);
if (err) { if (err) {
// locked by vfs_lookup // locked by vfs_lookup
VOP_UNLOCK(mounton); VOP_UNLOCK(mounton);
@@ -487,22 +487,15 @@ int vfs_unlink(vnode_t *ref, char *path) {
goto cleanup; goto cleanup;
} }
// TODO auth check err = auth_filesystem_check(getcred(), AUTH_ACTIONS_FILESYSTEM_UNLINK, child, parent) ? EACCES : 0;
if (0) { if (err)
// locked by VOP_LOOKUP goto cleanup_release;
VOP_UNLOCK(child);
// locked by vfs_lookup, unlocked by VOP_LOOKUP if component is ".."
if (!isdotdot)
VOP_UNLOCK(parent);
VOP_RELEASE(child);
VOP_RELEASE(parent);
return err;
}
err = VOP_UNLINK(parent, child, component, getcred()); err = VOP_UNLINK(parent, child, component, getcred());
cleanup_release:
// locked by VOP_LOOKUP // locked by VOP_LOOKUP
VOP_UNLOCK(child); VOP_UNLOCK(child);
// locked by vfs_lookup, unlocked by VOP_LOOKUP if component is ".." // locked by vfs_lookup, unlocked by VOP_LOOKUP if component is ".."
if (!isdotdot) if (!isdotdot)
VOP_UNLOCK(parent); VOP_UNLOCK(parent);
@@ -570,7 +563,6 @@ int vfs_rename(vnode_t *srcref, char *srcpath, vnode_t *dstref, char *dstpath, i
if (src != dst) if (src != dst)
VOP_LOCK(src); // lock it again for the rename if they arent the same VOP_LOCK(src); // lock it again for the rename if they arent the same
// TODO auth check
if (0) if (0)
goto cleanup_child; goto cleanup_child;
+3 -2
View File
@@ -25,6 +25,7 @@
#define AUTH_ACTIONS_FILESYSTEM_READ 4 #define AUTH_ACTIONS_FILESYSTEM_READ 4
#define AUTH_ACTIONS_FILESYSTEM_SETATTR 8 #define AUTH_ACTIONS_FILESYSTEM_SETATTR 8
#define AUTH_ACTIONS_FILESYSTEM_MOUNT 16 #define AUTH_ACTIONS_FILESYSTEM_MOUNT 16
#define AUTH_ACTIONS_FILESYSTEM_UNLINK 32
#define AUTH_ACTIONS_CRED_SETRESUID 1 #define AUTH_ACTIONS_CRED_SETRESUID 1
#define AUTH_ACTIONS_CRED_SETRESGID 2 #define AUTH_ACTIONS_CRED_SETRESGID 2
@@ -44,8 +45,8 @@ typedef struct {
void auth_registerlistener(int scope, authlistener_t listener); void auth_registerlistener(int scope, authlistener_t listener);
int auth_check(int scope, cred_t *cred, int actions, void *arg0, void *arg1, void *arg2); int auth_check(int scope, cred_t *cred, int actions, void *arg0, void *arg1, void *arg2);
static inline int auth_filesystem_check(cred_t *cred, int actions, vnode_t *vnode) { static inline int auth_filesystem_check(cred_t *cred, int actions, vnode_t *vnode, vnode_t *dirvnode) {
return auth_check(AUTH_SCOPE_FILESYSTEM, cred, actions, vnode, NULL, NULL); return auth_check(AUTH_SCOPE_FILESYSTEM, cred, actions, vnode, dirvnode, NULL);
} }
static inline int auth_system_check(cred_t *cred, int actions) { static inline int auth_system_check(cred_t *cred, int actions) {
+40
View File
@@ -10,10 +10,15 @@
static int filesystem(cred_t *cred, int actions, void *arg0, void *arg1, void *arg2) { static int filesystem(cred_t *cred, int actions, void *arg0, void *arg1, void *arg2) {
vnode_t *vnode = arg0; // expected locked vnode_t *vnode = arg0; // expected locked
vnode_t *dirvnode = arg1; // expected locked
vattr_t attr; vattr_t attr;
VOP_GETATTR(vnode, &attr, NULL); VOP_GETATTR(vnode, &attr, NULL);
vattr_t dirattr;
if (dirvnode)
VOP_GETATTR(dirvnode, &dirattr, NULL);
int weight = 0; int weight = 0;
if (actions & AUTH_ACTIONS_FILESYSTEM_WRITE) { if (actions & AUTH_ACTIONS_FILESYSTEM_WRITE) {
if (CRED_IS_ESU(cred)) if (CRED_IS_ESU(cred))
@@ -77,8 +82,43 @@ static int filesystem(cred_t *cred, int actions, void *arg0, void *arg1, void *a
weight += 1; weight += 1;
else else
return AUTH_DECISION_DENY; return AUTH_DECISION_DENY;
DONE_CHECK(actions);
} }
if (actions & AUTH_ACTIONS_FILESYSTEM_UNLINK) {
__assert(dirvnode);
bool dosticky = false;
if (CRED_IS_ESU(cred)) {
dosticky = false;
weight += 1;
} else if (dirattr.uid == cred->euid && (dirattr.mode & V_ATTR_MODE_USER_WRITE)) {
dosticky = true;
} else if (dirattr.uid != cred->euid && dirattr.gid == cred->egid && (dirattr.mode & V_ATTR_MODE_GROUP_WRITE)) {
dosticky = true;
} else if (dirattr.uid != cred->euid && dirattr.gid != cred->egid && (dirattr.mode & V_ATTR_MODE_OTHERS_WRITE)) {
dosticky = true;
} else {
return AUTH_DECISION_DENY;
}
if (dosticky && (dirattr.mode & V_ATTR_MODE_STICKY)) {
// sticky directory, file can only be unlinked if:
// - user requesting deletion is root (already checked for)
// OR
// - user requesting deletion has write access (already checked for) AND
// - effective user requesting deletion is owner of directory
// OR
// - effective user requesting deletion is owner of file
if (dirattr.uid == cred->euid || attr.uid == cred->euid)
weight += 1;
else
return AUTH_DECISION_DENY;
}
DONE_CHECK(actions);
}
done: done:
return weight ? AUTH_DECISION_ALLOW : AUTH_DECISION_DEFER; return weight ? AUTH_DECISION_ALLOW : AUTH_DECISION_DEFER;
+1 -1
View File
@@ -10,7 +10,7 @@ static int dochmod(vnode_t *node, mode_t mode) {
vattr_t attr; vattr_t attr;
cred_t *cred = &_cpu()->thread->proc->cred; cred_t *cred = &_cpu()->thread->proc->cred;
int e = auth_filesystem_check(cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node); int e = auth_filesystem_check(cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node, NULL);
if (e) if (e)
return e; return e;
+1 -1
View File
@@ -50,7 +50,7 @@ syscallret_t syscall_fchownat(context_t *, int fd, char *upath, uid_t owner, gid
goto cleanup; goto cleanup;
} }
ret.errno = auth_filesystem_check(&_cpu()->thread->proc->cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node); ret.errno = auth_filesystem_check(&_cpu()->thread->proc->cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node, NULL);
if (ret.errno) { if (ret.errno) {
VOP_UNLOCK(node); VOP_UNLOCK(node);
goto cleanup; goto cleanup;
+1 -1
View File
@@ -71,7 +71,7 @@ syscallret_t syscall_utimensat(context_t *, int fd, char *upath, timespec_t uts[
attr.atime = ts[0]; attr.atime = ts[0];
attr.mtime = ts[1]; attr.mtime = ts[1];
ret.errno = auth_filesystem_check(&_cpu()->thread->proc->cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node); ret.errno = auth_filesystem_check(&_cpu()->thread->proc->cred, AUTH_ACTIONS_FILESYSTEM_SETATTR, node, NULL);
if (ret.errno) { if (ret.errno) {
if (uts == NULL) if (uts == NULL)
ret.errno = VOP_ACCESS(node, V_ACCESS_WRITE, &_cpu()->thread->proc->cred); ret.errno = VOP_ACCESS(node, V_ACCESS_WRITE, &_cpu()->thread->proc->cred);