xiaoxiang781216 commented on code in PR #19900:
URL: https://github.com/apache/nuttx/pull/19900#discussion_r3925733051
##########
fs/vfs/CMakeLists.txt:
##########
@@ -54,6 +54,10 @@ set(SRCS
fs_truncate.c
fs_link.c)
+if(CONFIG_FS_CHROOT)
Review Comment:
move this patch after `fs: start absolute lookups at the jail root`
##########
fs/vfs/fs_chroot.c:
##########
@@ -0,0 +1,131 @@
+/****************************************************************************
+ * fs/vfs/fs_chroot.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: chroot
+ *
+ * Description:
+ * Cause the named directory to become the root directory, that is, the
+ * starting point for path names beginning with '/'.
+ *
+ * Input Parameters:
+ * path - Directory to use as the new root
+ *
+ * Returned Value:
+ * 0(OK) on success; -1(ERROR) on failure with errno set appropriately.
+ *
+ ****************************************************************************/
+
+int chroot(FAR const char *path)
+{
+ FAR struct tcb_s *rtcb;
+ FAR struct task_group_s *group;
+ FAR char *newroot;
+ char abspath[PATH_MAX];
+ struct stat buf;
+ int ret;
+
+ if (path == NULL || path[0] == '\0')
+ {
+ set_errno(ENOENT);
+ return ERROR;
+ }
+
+ rtcb = nxsched_self();
+ DEBUGASSERT(rtcb != NULL && rtcb->group != NULL);
+ group = rtcb->group;
+
+#ifdef CONFIG_SCHED_USER_IDENTITY
+ if (group->tg_euid != 0)
+ {
+ set_errno(EPERM);
+ return ERROR;
+ }
+#endif
+
+ ret = nx_stat(path, &buf, 1);
+ if (ret < 0)
+ {
+ set_errno(-ret);
+ return ERROR;
+ }
+
+ if (!S_ISDIR(buf.st_mode))
+ {
+ set_errno(ENOTDIR);
+ return ERROR;
+ }
+
+ /* Resolve to a host absolute path the same way lookups do: make
+ * absolute, prepend the current jail, and normalize. No second
+ * inode walk.
+ */
+
+ ret = inode_chroot_hostpath(path, abspath, sizeof(abspath));
+ if (ret < 0)
+ {
+ set_errno(-ret);
+ return ERROR;
+ }
+
+ /* Host "/" means no jail. Clear any previous root. */
+
+ if (strcmp(abspath, "/") == 0)
+ {
+ kmm_free(group->tg_root);
+ group->tg_root = NULL;
+ return OK;
+ }
+
+ newroot = strdup(abspath);
Review Comment:
fs_heap_strdup
##########
fs/mount/fs_automount.c:
##########
@@ -440,7 +446,7 @@ static int automount_findinode(FAR const char *path)
/* Relinquish our exclusive access to the inode try and return the result */
inode_runlock();
Review Comment:
need release inode
##########
fs/vfs/fs_rename.c:
##########
@@ -174,38 +179,58 @@ static int pseudorename(FAR const char *oldpath, FAR
struct inode *oldinode,
ret = inode_remove(newpath);
if (ret < 0 && ret != -EBUSY)
{
- goto errout_with_lock;
+ goto errout_with_newinode;
}
#ifdef CONFIG_FS_NOTIFY
notify_unlink(newpath);
#endif
}
}
+ else
+ {
+ newinode = NULL;
Review Comment:
why zero again
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]