Skip to content

Commit

Permalink
paths: Reject dotdots above root
Browse files Browse the repository at this point in the history
This changes the behavior of paths that attempt to navigate above root
to return LFS_ERR_INVAL:
- before: lfs_stat("..") => 0
- after:  lfs_stat("..") => LFS_ERR_INVAL

This is a bit of an opinionated change while making other path
resolution tweaks.

---

It's a bit unclear exactly what dotdots above the root should do.

POSIX notes: "As a special case, in the root directory, dot-dot may
refer to the root directory itself.", but the word choice of "may"
implies it is up to the implementation.

Originally I implemented this as a loop-root because that's what my
Linux machine does, but I think this may have been the wrong choice.
Because, well, in what world should "/.." not be an error?

Long-term, this should help be more consistent with openat-like APIs,
where ".." should either error with LFS_ERR_INVAL or actually navigate
above the relative path. "Saturating" the ".." navigation really doesn't
make sense in this case.
  • Loading branch information
geky committed Nov 23, 2024
1 parent b393265 commit b0d65c5
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 182 deletions.
10 changes: 7 additions & 3 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1500,13 +1500,17 @@ static lfs_stag_t lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
}
lfs_size_t namelen = strcspn(name, "/");

// skip '.' and root '..'
if ((namelen == 1 && memcmp(name, ".", 1) == 0) ||
(namelen == 2 && memcmp(name, "..", 2) == 0)) {
// skip '.'
if (namelen == 1 && memcmp(name, ".", 1) == 0) {
name += namelen;
goto nextname;
}

// error on unmatched '..', trying to go above root?
if (namelen == 2 && memcmp(name, "..", 2) == 0) {
return LFS_ERR_INVAL;
}

// skip if matched by '..' in name
const char *suffix = name + namelen;
lfs_size_t sufflen;
Expand Down
Loading

0 comments on commit b0d65c5

Please sign in to comment.