-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fs/vfs: add parent-directory permission checks for unlink, mkdir and rename #18902
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
Open
Abhishekmishra2808
wants to merge
1
commit into
apache:master
Choose a base branch
from
Abhishekmishra2808:fs/inode-checkperm-vfs-ops
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−77
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| #include <assert.h> | ||
| #include <errno.h> | ||
| #include <fcntl.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <nuttx/fs/fs.h> | ||
| #include <nuttx/rwsem.h> | ||
|
|
@@ -48,6 +49,70 @@ | |
|
|
||
| static rw_semaphore_t g_inode_lock = RWSEM_INITIALIZER; | ||
|
|
||
| /**************************************************************************** | ||
| * Private Functions | ||
| ****************************************************************************/ | ||
|
|
||
| /**************************************************************************** | ||
| * Name: _inode_checkmode | ||
| * | ||
| * Description: | ||
| * Test effective credentials against 'inode' for 'amode' access. | ||
| * Kernel threads always pass. | ||
| * | ||
| * Returned Value: | ||
| * Zero (OK) or -EACCES. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| #if defined(CONFIG_PSEUDOFS_ATTRIBUTES) && defined(CONFIG_SCHED_USER_IDENTITY) | ||
| static int _inode_checkmode(FAR struct inode *inode, int amode) | ||
| { | ||
| FAR struct tcb_s *rtcb; | ||
| mode_t perm; | ||
| uid_t uid; | ||
| gid_t gid; | ||
|
|
||
| /* Kernel threads are always granted access */ | ||
|
|
||
| rtcb = nxsched_self(); | ||
| if ((rtcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL) | ||
| { | ||
| return OK; | ||
| } | ||
|
|
||
| /* Use effective credentials */ | ||
|
|
||
| DEBUGASSERT(rtcb->group != NULL); | ||
| uid = rtcb->group->tg_euid; | ||
| gid = rtcb->group->tg_egid; | ||
|
|
||
| /* Select the applicable permission-bit triplet */ | ||
|
|
||
| if (uid == inode->i_owner) | ||
| { | ||
| perm = (inode->i_mode >> 6) & 7; | ||
| } | ||
| else if (gid == inode->i_group) | ||
| { | ||
| perm = (inode->i_mode >> 3) & 7; | ||
| } | ||
| else | ||
| { | ||
| perm = inode->i_mode & 7; | ||
| } | ||
|
|
||
| /* Every requested bit must be present in the selected triplet */ | ||
|
|
||
| if ((amode & perm) != amode) | ||
| { | ||
| return -EACCES; | ||
| } | ||
|
|
||
| return OK; | ||
| } | ||
| #endif /* CONFIG_PSEUDOFS_ATTRIBUTES && CONFIG_SCHED_USER_IDENTITY */ | ||
|
|
||
| /**************************************************************************** | ||
| * Public Functions | ||
| ****************************************************************************/ | ||
|
|
@@ -124,49 +189,48 @@ void inode_runlock(void) | |
| * Name: inode_checkperm | ||
| * | ||
| * Description: | ||
| * Validate that 'inode' can be opened with the access described by | ||
| * 'oflags'. Two sequential checks are performed: | ||
| * | ||
| * 1. Operation-support check (all inode types, unconditional): | ||
| * Verifies the driver exposes the read/write entry points required by | ||
| * 'oflags'. Returns -ENXIO when ops are NULL and -EACCES when the | ||
| * required entry point is absent. Pseudo-directory inodes | ||
| * (INODE_IS_PSEUDODIR) are exempted from this step. | ||
| * | ||
| * 2. UNIX permission check (pseudo-filesystem inodes only): | ||
| * Compares effective uid/gid against i_mode owner/group/other bits. | ||
| * Mountpoint inodes and kernel threads are unconditionally exempted. | ||
| * Requires CONFIG_PSEUDOFS_ATTRIBUTES and CONFIG_SCHED_USER_IDENTITY; | ||
| * when either option is disabled this step is a no-op. | ||
| * Validate open access to 'inode' for 'oflags'. Checks driver operation | ||
| * support, then pseudo-filesystem mode bits when enabled. Mountpoints | ||
| * are exempt from mode checks. | ||
| * | ||
| * Input Parameters: | ||
| * inode - The inode to check | ||
| * oflags - Open flags (O_RDONLY / O_WRONLY / O_RDWR) | ||
| * | ||
| * Returned Value: | ||
| * Zero (OK) on success. Negated errno on failure: | ||
| * -ENXIO ops pointer is NULL | ||
| * -EACCES required operation not supported, or permission denied | ||
| * Zero (OK) on success, or a negated errno on failure. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| int inode_checkperm(FAR struct inode *inode, int oflags) | ||
| { | ||
| #if defined(CONFIG_PSEUDOFS_ATTRIBUTES) && defined(CONFIG_SCHED_USER_IDENTITY) | ||
| FAR struct tcb_s *rtcb; | ||
| mode_t perm; | ||
| uid_t uid; | ||
| gid_t gid; | ||
| int amode = 0; | ||
| #endif | ||
| FAR const struct file_operations *ops; | ||
|
|
||
| /* === Step 1: operation-support check === */ | ||
|
|
||
| /* Pseudo-directories carry no ops and are always accessible */ | ||
|
|
||
| if (INODE_IS_PSEUDODIR(inode)) | ||
| { | ||
| #if defined(CONFIG_PSEUDOFS_ATTRIBUTES) && defined(CONFIG_SCHED_USER_IDENTITY) | ||
| if (INODE_IS_MOUNTPT(inode)) | ||
|
xiaoxiang781216 marked this conversation as resolved.
|
||
| { | ||
| return OK; | ||
| } | ||
|
|
||
| if ((oflags & O_RDOK) != 0) | ||
|
Contributor
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. move out of if/else to avoid the dup with line 257-265 |
||
| { | ||
| amode |= R_OK; | ||
| } | ||
|
|
||
| if ((oflags & O_WROK) != 0) | ||
| { | ||
| amode |= W_OK; | ||
| } | ||
|
|
||
| return _inode_checkmode(inode, amode); | ||
| #else | ||
| return OK; | ||
| #endif | ||
| } | ||
|
|
||
| ops = inode->u.i_ops; | ||
|
|
@@ -185,61 +249,61 @@ int inode_checkperm(FAR struct inode *inode, int oflags) | |
|
|
||
| #if defined(CONFIG_PSEUDOFS_ATTRIBUTES) && defined(CONFIG_SCHED_USER_IDENTITY) | ||
|
|
||
| /* === Step 2: UNIX permission check (pseudo-filesystem inodes only) === */ | ||
|
|
||
| /* Mountpoints delegate permission enforcement to the underlying | ||
| * filesystem | ||
| */ | ||
|
|
||
| if (INODE_IS_MOUNTPT(inode)) | ||
| { | ||
| return OK; | ||
| } | ||
|
|
||
| /* Kernel threads are always granted access */ | ||
| if ((oflags & O_RDOK) != 0) | ||
| { | ||
| amode |= R_OK; | ||
| } | ||
|
|
||
| rtcb = nxsched_self(); | ||
| if ((rtcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL) | ||
| if ((oflags & O_WROK) != 0) | ||
| { | ||
| return OK; | ||
| amode |= W_OK; | ||
| } | ||
|
|
||
| /* Use effective credentials */ | ||
| return _inode_checkmode(inode, amode); | ||
|
|
||
| DEBUGASSERT(rtcb->group != NULL); | ||
| uid = rtcb->group->tg_euid; | ||
| gid = rtcb->group->tg_egid; | ||
| #endif /* CONFIG_PSEUDOFS_ATTRIBUTES && CONFIG_SCHED_USER_IDENTITY */ | ||
|
|
||
| /* Select the applicable permission-bit triplet */ | ||
| return OK; | ||
| } | ||
|
|
||
| if (uid == inode->i_owner) | ||
| { | ||
| perm = (inode->i_mode >> 6) & 7; | ||
| } | ||
| else if (gid == inode->i_group) | ||
| { | ||
| perm = (inode->i_mode >> 3) & 7; | ||
| } | ||
| else | ||
| { | ||
| perm = inode->i_mode & 7; | ||
| } | ||
| /**************************************************************************** | ||
| * Name: inode_checkdirperm | ||
| * | ||
| * Description: | ||
| * Check parent directory 'dir' for 'amode' access on pseudo-filesystem | ||
| * inodes. NULL 'dir' (root) and mountpoints are exempt. | ||
| * | ||
| * Input Parameters: | ||
| * dir - Parent directory inode, or NULL for a root-level path | ||
| * amode - Access mode bitmask (R_OK / W_OK / X_OK) | ||
| * | ||
| * Returned Value: | ||
| * Zero (OK) on success, or -EACCES if permission is denied. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| /* Bit 2 (value 4) = read permission */ | ||
| int inode_checkdirperm(FAR struct inode *dir, int amode) | ||
| { | ||
| #if defined(CONFIG_PSEUDOFS_ATTRIBUTES) && defined(CONFIG_SCHED_USER_IDENTITY) | ||
|
|
||
| if (((oflags & O_RDOK) != 0) && ((perm & 4) == 0)) | ||
| if (dir == NULL) | ||
| { | ||
| return -EACCES; | ||
| return OK; | ||
| } | ||
|
|
||
| /* Bit 1 (value 2) = write permission */ | ||
|
|
||
| if (((oflags & O_WROK) != 0) && ((perm & 2) == 0)) | ||
| if (INODE_IS_MOUNTPT(dir)) | ||
| { | ||
| return -EACCES; | ||
| return OK; | ||
| } | ||
|
|
||
| #endif /* CONFIG_PSEUDOFS_ATTRIBUTES && CONFIG_SCHED_USER_IDENTITY */ | ||
| return _inode_checkmode(dir, amode); | ||
|
|
||
| #else | ||
| return OK; | ||
| #endif /* CONFIG_PSEUDOFS_ATTRIBUTES && CONFIG_SCHED_USER_IDENTITY */ | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.