Skip to content

Commit 573cfac

Browse files
committed
main: prevent creating files longer than f_namemax
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
1 parent 4725e0b commit 573cfac

File tree

2 files changed

+91
-13
lines changed

2 files changed

+91
-13
lines changed

main.c

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ open_by_handle_at (int mount_fd, struct file_handle *handle, int flags)
145145
#define PRIVILEGED_OPAQUE_XATTR "trusted.overlay.opaque"
146146
#define PRIVILEGED_ORIGIN_XATTR "trusted.overlay.origin"
147147
#define OPAQUE_WHITEOUT ".wh..wh..opq"
148-
#define WHITEOUT_MAX_LEN (sizeof (OPAQUE_WHITEOUT))
148+
#define WHITEOUT_MAX_LEN (sizeof (".wh.")-1)
149149

150150
#if !defined FICLONE && defined __linux__
151151
# define FICLONE _IOW (0x94, 9, int)
@@ -3583,6 +3583,43 @@ do_getattr (fuse_req_t req, struct fuse_entry_param *e, struct ovl_node *node, i
35833583
return 0;
35843584
}
35853585

3586+
static int
3587+
do_statfs (struct ovl_data *lo, struct statvfs *sfs)
3588+
{
3589+
int ret, fd;
3590+
3591+
fd = get_first_layer (lo)->fd;
3592+
3593+
if (fd >= 0)
3594+
ret = fstatvfs (fd, sfs);
3595+
else
3596+
ret = statvfs (lo->mountpoint, sfs);
3597+
if (ret < 0)
3598+
return ret;
3599+
3600+
sfs->f_namemax -= WHITEOUT_MAX_LEN;
3601+
return 0;
3602+
}
3603+
3604+
static short
3605+
get_fs_namemax (struct ovl_data *lo)
3606+
{
3607+
static short namemax = 0;
3608+
if (namemax == 0)
3609+
{
3610+
struct statvfs sfs;
3611+
int ret;
3612+
3613+
ret = do_statfs (lo, &sfs);
3614+
/* On errors use a sane default. */
3615+
if (ret < 0)
3616+
namemax = 255 - WHITEOUT_MAX_LEN;
3617+
else
3618+
namemax = sfs.f_namemax;
3619+
}
3620+
return namemax;
3621+
}
3622+
35863623
static void
35873624
ovl_create (fuse_req_t req, fuse_ino_t parent, const char *name,
35883625
mode_t mode, struct fuse_file_info *fi)
@@ -3591,12 +3628,19 @@ ovl_create (fuse_req_t req, fuse_ino_t parent, const char *name,
35913628
cleanup_close int fd = -1;
35923629
struct fuse_entry_param e;
35933630
struct ovl_node *node = NULL;
3631+
struct ovl_data *lo = ovl_data (req);
35943632
struct stat st;
35953633

35963634
if (UNLIKELY (ovl_debug (req)))
35973635
fprintf (stderr, "ovl_create(parent=%" PRIu64 ", name=%s)\n",
35983636
parent, name);
35993637

3638+
if (strlen (name) > get_fs_namemax (lo))
3639+
{
3640+
fuse_reply_err (req, ENAMETOOLONG);
3641+
return;
3642+
}
3643+
36003644
fi->flags = fi->flags | O_CREAT;
36013645

36023646
fd = ovl_do_open (req, parent, name, fi->flags, mode, &node, &st);
@@ -3869,6 +3913,12 @@ ovl_link (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, const char *newn
38693913
if (UNLIKELY (ovl_debug (req)))
38703914
fprintf (stderr, "ovl_link(ino=%" PRIu64 "s, newparent=%" PRIu64 "s, newname=%s)\n", ino, newparent, newname);
38713915

3916+
if (strlen (newname) > get_fs_namemax (lo))
3917+
{
3918+
fuse_reply_err (req, ENAMETOOLONG);
3919+
return;
3920+
}
3921+
38723922
node = do_lookup_file (lo, ino, NULL);
38733923
if (node == NULL || node->whiteout)
38743924
{
@@ -4008,6 +4058,12 @@ ovl_symlink (fuse_req_t req, const char *link, fuse_ino_t parent, const char *na
40084058
if (UNLIKELY (ovl_debug (req)))
40094059
fprintf (stderr, "ovl_symlink(link=%s, ino=%" PRIu64 "s, name=%s)\n", link, parent, name);
40104060

4061+
if (strlen (name) > get_fs_namemax (lo))
4062+
{
4063+
fuse_reply_err (req, ENAMETOOLONG);
4064+
return;
4065+
}
4066+
40114067
pnode = do_lookup_file (lo, parent, NULL);
40124068
if (pnode == NULL || pnode->whiteout)
40134069
{
@@ -4411,9 +4467,17 @@ ovl_rename (fuse_req_t req, fuse_ino_t parent, const char *name,
44114467
unsigned int flags)
44124468
{
44134469
cleanup_lock int l = enter_big_lock ();
4470+
struct ovl_data *lo = ovl_data (req);
4471+
44144472
if (UNLIKELY (ovl_debug (req)))
44154473
fprintf (stderr, "ovl_rename(ino=%" PRIu64 "s, name=%s , ino=%" PRIu64 "s, name=%s)\n", parent, name, newparent, newname);
44164474

4475+
if (strlen (newname) > get_fs_namemax (lo))
4476+
{
4477+
fuse_reply_err (req, ENAMETOOLONG);
4478+
return;
4479+
}
4480+
44174481
if (flags & RENAME_EXCHANGE)
44184482
ovl_rename_exchange (req, parent, name, newparent, newname, flags);
44194483
else
@@ -4423,27 +4487,17 @@ ovl_rename (fuse_req_t req, fuse_ino_t parent, const char *name,
44234487
static void
44244488
ovl_statfs (fuse_req_t req, fuse_ino_t ino)
44254489
{
4426-
int ret, fd;
4490+
int ret;
44274491
struct statvfs sfs;
44284492
struct ovl_data *lo = ovl_data (req);
44294493

4430-
if (UNLIKELY (ovl_debug (req)))
4431-
fprintf (stderr, "ovl_statfs(ino=%" PRIu64 "s)\n", ino);
4432-
4433-
fd = get_first_layer (lo)->fd;
4434-
4435-
if (fd >= 0)
4436-
ret = fstatvfs (fd, &sfs);
4437-
else
4438-
ret = statvfs (lo->mountpoint, &sfs);
4494+
ret = do_statfs (lo, &sfs);
44394495
if (ret < 0)
44404496
{
44414497
fuse_reply_err (req, errno);
44424498
return;
44434499
}
44444500

4445-
sfs.f_namemax -= WHITEOUT_MAX_LEN;
4446-
44474501
fuse_reply_statfs (req, &sfs);
44484502
}
44494503

@@ -4555,6 +4609,12 @@ ovl_mknod (fuse_req_t req, fuse_ino_t parent, const char *name, mode_t mode, dev
45554609
fprintf (stderr, "ovl_mknod(ino=%" PRIu64 ", name=%s, mode=%d, rdev=%lu)\n",
45564610
parent, name, mode, rdev);
45574611

4612+
if (strlen (name) > get_fs_namemax (lo))
4613+
{
4614+
fuse_reply_err (req, ENAMETOOLONG);
4615+
return;
4616+
}
4617+
45584618
mode = mode & ~ctx->umask;
45594619

45604620
node = do_lookup_file (lo, parent, name);
@@ -4665,6 +4725,13 @@ ovl_mkdir (fuse_req_t req, fuse_ino_t parent, const char *name, mode_t mode)
46654725
fprintf (stderr, "ovl_mkdir(ino=%" PRIu64 ", name=%s, mode=%d)\n",
46664726
parent, name, mode);
46674727

4728+
4729+
if (strlen (name) > get_fs_namemax (lo))
4730+
{
4731+
fuse_reply_err (req, ENAMETOOLONG);
4732+
return;
4733+
}
4734+
46684735
node = do_lookup_file (lo, parent, name);
46694736
if (node != NULL && !node->whiteout)
46704737
{

tests/fedora-installs.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,14 @@ mkdir merged/adir
188188
touch -h -d "2020-01-02 10:11:12" merged/adir
189189
stat --format "%y" merged/adir | grep "10:11:12"
190190
stat --format "%x" merged/adir | grep "10:11:12"
191+
192+
upper_max_filename_len=$(stat -f -c %l upper)
193+
merged_max_filename_len=$(stat -f -c %l merged)
194+
195+
test $merged_max_filename_len -lt $upper_max_filename_len
196+
197+
if touch merged/$(printf %${upper_max_filename_len}s | tr ' ' A}); then
198+
exit 1
199+
fi
200+
201+
touch merged/$(printf %${merged_max_filename_len}s | tr ' ' A})

0 commit comments

Comments
 (0)