--- sys/fs/fdescfs/fdesc_vnops.c.orig +++ sys/fs/fdescfs/fdesc_vnops.c @@ -73,11 +73,12 @@ static vop_readlink_t fdesc_readlink; static vop_reclaim_t fdesc_reclaim; static vop_setattr_t fdesc_setattr; +static vop_access_t fdesc_access; static struct vop_vector fdesc_vnodeops = { .vop_default = &default_vnodeops, - .vop_access = VOP_NULL, + .vop_access = fdesc_access, .vop_getattr = fdesc_getattr, .vop_lookup = fdesc_lookup, .vop_open = fdesc_open, @@ -524,19 +525,24 @@ return (EACCES); fd = VTOFDESC(ap->a_vp)->fd_fd; + cap_rights_init(&rights, CAP_FUTIMES, CAP_FCHMOD, CAP_FCHOWN, + CAP_FCHFLAGS); /* * Allow setattr where there is an underlying vnode. * For O_PATH descriptors, disallow truncate. */ if (vap->va_size != VNOVAL) { - error = getvnode(td, fd, - cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp); + cap_rights_set(&rights, CAP_FTRUNCATE); + error = getvnode(td, fd, &rights, &fp); + if (error == 0 && (fp->f_flag & FWRITE) == 0) { + error = EACCES; + goto out; + } } else { - error = getvnode_path(td, fd, - cap_rights_init_one(&rights, CAP_EXTATTR_SET), NULL, &fp); + error = getvnode_path(td, fd, &rights, NULL, &fp); } - if (error) { + if (error != 0) { /* * getvnode() returns EINVAL if the file descriptor is not * backed by a vnode. Silently drop all changes except @@ -557,7 +563,10 @@ VOP_UNLOCK(vp); vn_finished_write(mp); } +out: + VOP_UNLOCK(ap->a_vp); fdrop(fp, td); + vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); return (error); } @@ -704,3 +713,34 @@ vn_lock(vn, lockflags | LK_RETRY); return (error); } + +static int +fdesc_access(struct vop_access_args *va) +{ + struct vnode *vp; + struct fdescnode *fd; + struct file *fp; + int error, flags, locked; + + vp = va->a_vp; + fd = VTOFDESC(vp); + if (fd->fd_type == Froot) + return (0); + error = fget(va->a_td, fd->fd_fd, &cap_no_rights, &fp); + if (error != 0) + return (error); + flags = 0; + if ((va->a_accmode & VREAD) != 0) + flags |= FREAD; + if ((va->a_accmode & (VWRITE | VAPPEND)) != 0) + flags |= FWRITE; + if ((va->a_accmode & VEXEC) != 0) + flags |= FEXEC; + if ((flags & fp->f_flag) != flags) + error = EACCES; + locked = VOP_ISLOCKED(vp); + VOP_UNLOCK(vp); + fdrop(fp, va->a_td); + vn_lock(vp, locked | LK_RETRY); + return (error); +} --- tests/sys/fs/fdescfs/fdescfs_test.c.orig +++ tests/sys/fs/fdescfs/fdescfs_test.c @@ -223,6 +223,77 @@ ATF_REQUIRE_EQ(0, unmount("mnt", 0)); } +#define TEXT "Hello, world!\n" +#define LEN 14 + +FDESCFS_TC(truncate_ro, + "disallow truncating read-only files (plain)"); +ATF_TC_BODY(truncate_ro, tc) +{ + struct stat sb; + char fd_path[16]; + int fd; + + /* create read-only target file */ + ATF_REQUIRE((fd = open("file", O_RDWR | O_CREAT, 0400)) >= 0); + ATF_REQUIRE_EQ(LEN, write(fd, TEXT, LEN)); + ATF_REQUIRE_EQ(0, close(fd)); + + /* reopen the file read-only */ + ATF_REQUIRE((fd = open("file", O_RDONLY)) >= 0); + + /* mount fdescfs */ + mount_fdescfs(NULL); + + /* construct and stat path */ + snprintf(fd_path, sizeof(fd_path), "mnt/%d", fd); + ATF_REQUIRE_EQ(0, stat(fd_path, &sb)); + ATF_CHECK_EQ(S_IFCHR, sb.st_mode & S_IFMT); + ATF_CHECK_EQ(0555, sb.st_mode & ALLPERMS); + ATF_CHECK_EQ(0, sb.st_size); + + /* attempt to truncate */ + ATF_CHECK(truncate(fd_path, 0) < 0); + ATF_CHECK_EQ(LEN, lseek(fd, 0, SEEK_END)); + + /* done */ + ATF_CHECK_EQ(0, close(fd)); +} + +FDESCFS_TC(nodup_truncate_ro, + "allow truncating read-only files (nodup)"); +ATF_TC_BODY(nodup_truncate_ro, tc) +{ + struct stat sb; + char fd_path[16]; + int fd; + + /* create read-only target file */ + ATF_REQUIRE((fd = open("file", O_RDWR | O_CREAT, 0400)) >= 0); + ATF_REQUIRE_EQ(LEN, write(fd, TEXT, LEN)); + ATF_REQUIRE_EQ(0, close(fd)); + + /* reopen the file read-only */ + ATF_REQUIRE((fd = open("file", O_RDONLY)) >= 0); + + /* mount fdescfs */ + mount_fdescfs(nodup); + + /* construct and stat path */ + snprintf(fd_path, sizeof(fd_path), "mnt/%d", fd); + ATF_REQUIRE_EQ(0, stat(fd_path, &sb)); + ATF_CHECK_EQ(S_IFREG, sb.st_mode & S_IFMT); + ATF_CHECK_EQ(0400, sb.st_mode & ALLPERMS); + ATF_CHECK_EQ(LEN, sb.st_size); + + /* attempt to truncate */ + ATF_CHECK(truncate(fd_path, LEN / 2) == 0); + ATF_CHECK_EQ(LEN / 2, lseek(fd, 0, SEEK_END)); + + /* done */ + ATF_CHECK_EQ(0, close(fd)); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, cap_rights); @@ -232,5 +303,7 @@ ATF_TP_ADD_TC(tp, ioctl_caps); ATF_TP_ADD_TC(tp, nodup_ioctl_caps); ATF_TP_ADD_TC(tp, nodup_ioctl_cap_intersection); + ATF_TP_ADD_TC(tp, truncate_ro); + ATF_TP_ADD_TC(tp, nodup_truncate_ro); return (atf_no_error()); }