]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/ramfs: Support setting file mode on creation
authorAndrei Tatar <andrei@unikraft.io>
Mon, 24 Jul 2023 16:07:06 +0000 (18:07 +0200)
committerUnikraft <monkey@unikraft.io>
Mon, 7 Aug 2023 11:49:21 +0000 (11:49 +0000)
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 <andrei@unikraft.io>
Reviewed-by: Maria Sfiraiala <maria.sfiraiala@gmail.com>
Reviewed-by: Radu Nichita <radunichita99@gmail.com>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #997

lib/ramfs/ramfs.h
lib/ramfs/ramfs_vfsops.c
lib/ramfs/ramfs_vnops.c

index b4d5e0f53337b4cc91823db2b0cc9339100f3c76..a1de27f970901a0a5779b75c70427d473609bea6 100644 (file)
@@ -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.
index 15be8ce21cb97eeae41156b2243bcf51d0f13ebf..7e2840efc192ec4e25358fe9b3d1cdfba7ac733e 100644 (file)
@@ -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;
index 52b77f5851c00fde6c545ccabecf981b36139e8c..6585bbd0a95b3fe377ddf59ee210e49425631004 100644 (file)
@@ -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;