-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Landlock support to Firejail #5315
Changes from 2 commits
61b1544
ba828be
877fc99
460fa7a
c6d7474
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||
#define _GNU_SOURCE | ||||||||||||||||||||||||||||||||
#include <stdio.h> | ||||||||||||||||||||||||||||||||
#include <stddef.h> | ||||||||||||||||||||||||||||||||
#include <stdlib.h> | ||||||||||||||||||||||||||||||||
#include <unistd.h> | ||||||||||||||||||||||||||||||||
#include <fcntl.h> | ||||||||||||||||||||||||||||||||
#include <sys/syscall.h> | ||||||||||||||||||||||||||||||||
#include <sys/types.h> | ||||||||||||||||||||||||||||||||
#include <linux/landlock.h> | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int landlock_create_ruleset(struct landlock_ruleset_attr *rsattr,size_t size,__u32 flags) { | ||||||||||||||||||||||||||||||||
return syscall(__NR_landlock_create_ruleset,rsattr,size,flags); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int landlock_add_rule(int fd,enum landlock_rule_type t,void *attr,__u32 flags) { | ||||||||||||||||||||||||||||||||
return syscall(__NR_landlock_add_rule,fd,t,attr,flags); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int landlock_restrict_self(int fd,__u32 flags) { | ||||||||||||||||||||||||||||||||
int result = syscall(__NR_landlock_restrict_self,fd,flags); | ||||||||||||||||||||||||||||||||
if (result!=0) return result; | ||||||||||||||||||||||||||||||||
else { | ||||||||||||||||||||||||||||||||
close(fd); | ||||||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
Comment on lines
+24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The same applies to other |
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int create_full_ruleset() { | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All functions in this file should probably have a |
||||||||||||||||||||||||||||||||
struct landlock_ruleset_attr attr; | ||||||||||||||||||||||||||||||||
attr.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR | LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SOCK | LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | LANDLOCK_ACCESS_FS_MAKE_SYM | LANDLOCK_ACCESS_FS_EXECUTE; | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To make it readable. The same applies to the other |
||||||||||||||||||||||||||||||||
return landlock_create_ruleset(&attr,sizeof(attr),0); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int add_read_access_rule_by_path(int rset_fd,char *allowed_path) { | ||||||||||||||||||||||||||||||||
int result; | ||||||||||||||||||||||||||||||||
int allowed_fd = open(allowed_path,O_PATH | O_CLOEXEC); | ||||||||||||||||||||||||||||||||
struct landlock_path_beneath_attr target; | ||||||||||||||||||||||||||||||||
target.parent_fd = allowed_fd; | ||||||||||||||||||||||||||||||||
target.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR; | ||||||||||||||||||||||||||||||||
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0); | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are many whitespace issues. See this again: |
||||||||||||||||||||||||||||||||
close(allowed_fd); | ||||||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int add_write_access_rule_by_path(int rset_fd,char *allowed_path,int restricted) { | ||||||||||||||||||||||||||||||||
int result; | ||||||||||||||||||||||||||||||||
int allowed_fd = open(allowed_path,O_PATH | O_CLOEXEC); | ||||||||||||||||||||||||||||||||
struct landlock_path_beneath_attr target; | ||||||||||||||||||||||||||||||||
target.parent_fd = allowed_fd; | ||||||||||||||||||||||||||||||||
if (restricted==0) target.allowed_access = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SOCK | LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | LANDLOCK_ACCESS_FS_MAKE_SYM; | ||||||||||||||||||||||||||||||||
else if (restricted==1) target.allowed_access = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SYM; | ||||||||||||||||||||||||||||||||
else { | ||||||||||||||||||||||||||||||||
close(allowed_fd); | ||||||||||||||||||||||||||||||||
return -1; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0); | ||||||||||||||||||||||||||||||||
close(allowed_fd); | ||||||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
int add_execute_rule_by_path(int rset_fd,char *allowed_path) { | ||||||||||||||||||||||||||||||||
int result; | ||||||||||||||||||||||||||||||||
int allowed_fd = open(allowed_path,O_PATH | O_CLOEXEC); | ||||||||||||||||||||||||||||||||
struct landlock_path_beneath_attr target; | ||||||||||||||||||||||||||||||||
target.parent_fd = allowed_fd; | ||||||||||||||||||||||||||||||||
target.allowed_access = LANDLOCK_ACCESS_FS_EXECUTE; | ||||||||||||||||||||||||||||||||
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0); | ||||||||||||||||||||||||||||||||
close(allowed_fd); | ||||||||||||||||||||||||||||||||
return result; | ||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking the build on systems that do not support Landlock (such as on
Ubuntu 20.04, which uses an older kernel), even when Landlock is not enabled.
Partial error log of the codeql
analyze
job fromhttps://github.com/netblue30/firejail/runs/7853807421:
See
src/include/gcov_wrapper.h
and try to create alandlock_wrapper.h
in asimilar fashion. That is:
landlock_wrapper.h
, declare dummy functions ifHAVE_LANDLOCK
is notdefined.
landlock_wrapper.h
, only include"landlock_wrapper.h"
insteadof
<linux/landlock.h>
.landlock_wrapper.h
, avoid using#ifdef HAVE_LANDLOCK
where itcan be helped (just call the
landlock
functions as if LandLock is alwaysenabled).