From: Andrei Tatar Date: Mon, 24 Jul 2023 16:07:06 +0000 (+0200) Subject: lib/ramfs: Support setting file mode on creation X-Git-Tag: RELEASE-0.14.0~239 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=54df212e964bbbb317d22005719673a52bebf491;p=unikraft%2Funikraft.git lib/ramfs: Support setting file mode on creation Previously any file created on ramfs would have the mode bits set to 0777, regardless of the mode bits supplied to open/creat/mkdir, which can break software that explicitly checks these bits. This change corrects this oversight, having new files be created with the requested mode bits set. Signed-off-by: Andrei Tatar Reviewed-by: Maria Sfiraiala Reviewed-by: Radu Nichita Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #997 --- diff --git a/lib/ramfs/ramfs.h b/lib/ramfs/ramfs.h index b4d5e0f53..a1de27f97 100644 --- a/lib/ramfs/ramfs.h +++ b/lib/ramfs/ramfs.h @@ -82,10 +82,12 @@ struct ramfs_node { * @param type * The entry type (regular file - VREG, symbolic link - VLNK, * or directory - VDIR) + * @param mode + * The mode bits of the newly created node * @return * Pointer to the new ramfs_node */ -struct ramfs_node *ramfs_allocate_node(const char *name, int type); +struct ramfs_node *ramfs_allocate_node(const char *name, int type, mode_t mode); /** * Frees a ramfs node. diff --git a/lib/ramfs/ramfs_vfsops.c b/lib/ramfs/ramfs_vfsops.c index 15be8ce21..7e2840efc 100644 --- a/lib/ramfs/ramfs_vfsops.c +++ b/lib/ramfs/ramfs_vfsops.c @@ -82,7 +82,7 @@ ramfs_mount(struct mount *mp, const char *dev __unused, uk_pr_debug("%s: dev=%s\n", __func__, dev); /* Create a root node */ - np = ramfs_allocate_node("/", VDIR); + np = ramfs_allocate_node("/", VDIR, 0777); if (np == NULL) return ENOMEM; mp->m_root->d_vnode->v_data = np; diff --git a/lib/ramfs/ramfs_vnops.c b/lib/ramfs/ramfs_vnops.c index 52b77f585..6585bbd0a 100644 --- a/lib/ramfs/ramfs_vnops.c +++ b/lib/ramfs/ramfs_vnops.c @@ -75,7 +75,7 @@ set_times_to_now(struct timespec *time1, struct timespec *time2, } struct ramfs_node * -ramfs_allocate_node(const char *name, int type) +ramfs_allocate_node(const char *name, int type, mode_t mode) { struct ramfs_node *np; @@ -93,12 +93,13 @@ ramfs_allocate_node(const char *name, int type) strlcpy(np->rn_name, name, np->rn_namelen + 1); np->rn_type = type; + mode &= 0777; if (type == VDIR) - np->rn_mode = S_IFDIR|0777; + np->rn_mode = S_IFDIR|mode; else if (type == VLNK) np->rn_mode = S_IFLNK|0777; else - np->rn_mode = S_IFREG|0777; + np->rn_mode = S_IFREG|mode; set_times_to_now(&(np->rn_ctime), &(np->rn_atime), &(np->rn_mtime)); np->rn_owns_buf = true; @@ -117,11 +118,11 @@ ramfs_free_node(struct ramfs_node *np) } static struct ramfs_node * -ramfs_add_node(struct ramfs_node *dnp, const char *name, int type) +ramfs_add_node(struct ramfs_node *dnp, const char *name, int type, mode_t mode) { struct ramfs_node *np, *prev; - np = ramfs_allocate_node(name, type); + np = ramfs_allocate_node(name, type, mode); if (np == NULL) return NULL; @@ -264,7 +265,7 @@ ramfs_mkdir(struct vnode *dvp, const char *name, mode_t mode) if (!S_ISDIR(mode)) return EINVAL; - np = ramfs_add_node(dvp->v_data, name, VDIR); + np = ramfs_add_node(dvp->v_data, name, VDIR, mode); if (np == NULL) return ENOMEM; np->rn_size = 0; @@ -281,7 +282,7 @@ ramfs_symlink(struct vnode *dvp, const char *name, const char *link) if (strlen(name) > NAME_MAX) return ENAMETOOLONG; - np = ramfs_add_node(dvp->v_data, name, VLNK); + np = ramfs_add_node(dvp->v_data, name, VLNK, 0); if (np == NULL) return ENOMEM; @@ -389,7 +390,7 @@ ramfs_create(struct vnode *dvp, const char *name, mode_t mode) if (!S_ISREG(mode)) return EINVAL; - np = ramfs_add_node(dvp->v_data, name, VREG); + np = ramfs_add_node(dvp->v_data, name, VREG, mode); if (np == NULL) return ENOMEM; return 0; @@ -516,7 +517,8 @@ ramfs_rename(struct vnode *dvp1, struct vnode *vp1, const char *name1 __unused, } else { /* Create new file or directory */ old_np = vp1->v_data; - np = ramfs_add_node(dvp2->v_data, name2, old_np->rn_type); + np = ramfs_add_node(dvp2->v_data, name2, + old_np->rn_type, old_np->rn_mode); if (np == NULL) return ENOMEM;