1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup libcsupport |
---|
7 | * |
---|
8 | * @brief Change Root Directory |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com> |
---|
13 | * |
---|
14 | * COPYRIGHT (c) 1989-2008. |
---|
15 | * On-Line Applications Research Corporation (OAR). |
---|
16 | * |
---|
17 | * Modifications to support reference counting in the file system are |
---|
18 | * Copyright (c) 2012 embedded brains GmbH & Co. KG |
---|
19 | * |
---|
20 | * Redistribution and use in source and binary forms, with or without |
---|
21 | * modification, are permitted provided that the following conditions |
---|
22 | * are met: |
---|
23 | * 1. Redistributions of source code must retain the above copyright |
---|
24 | * notice, this list of conditions and the following disclaimer. |
---|
25 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
26 | * notice, this list of conditions and the following disclaimer in the |
---|
27 | * documentation and/or other materials provided with the distribution. |
---|
28 | * |
---|
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
30 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
39 | * POSSIBILITY OF SUCH DAMAGE. |
---|
40 | */ |
---|
41 | |
---|
42 | #ifdef HAVE_CONFIG_H |
---|
43 | #include "config.h" |
---|
44 | #endif |
---|
45 | |
---|
46 | #include <string.h> |
---|
47 | #include <unistd.h> |
---|
48 | |
---|
49 | #include <rtems/libio_.h> |
---|
50 | |
---|
51 | int chroot( const char *path ) |
---|
52 | { |
---|
53 | int rv = 0; |
---|
54 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
55 | rtems_filesystem_eval_path_context_t ctx; |
---|
56 | int eval_flags = RTEMS_FS_PERMS_EXEC |
---|
57 | | RTEMS_FS_FOLLOW_LINK; |
---|
58 | rtems_filesystem_location_info_t loc; |
---|
59 | rtems_filesystem_global_location_t *new_current_loc; |
---|
60 | |
---|
61 | /* |
---|
62 | * We use the global environment for path evaluation. This makes it possible |
---|
63 | * to escape from a chroot environment referencing an unmounted file system. |
---|
64 | */ |
---|
65 | rtems_filesystem_eval_path_start_with_root_and_current( |
---|
66 | &ctx, |
---|
67 | path, |
---|
68 | strlen( path ), |
---|
69 | eval_flags, |
---|
70 | &rtems_global_user_env.root_directory, |
---|
71 | &rtems_global_user_env.current_directory |
---|
72 | ); |
---|
73 | |
---|
74 | rtems_filesystem_eval_path_extract_currentloc( &ctx, &loc ); |
---|
75 | new_current_loc = rtems_filesystem_location_transform_to_global( &loc ); |
---|
76 | if ( !rtems_filesystem_global_location_is_null( new_current_loc ) ) { |
---|
77 | rtems_filesystem_global_location_t *new_root_loc = |
---|
78 | rtems_filesystem_global_location_obtain( &new_current_loc ); |
---|
79 | mode_t type = rtems_filesystem_location_type( &new_root_loc->location ); |
---|
80 | |
---|
81 | if ( S_ISDIR( type ) ) { |
---|
82 | sc = rtems_libio_set_private_env(); |
---|
83 | if (sc == RTEMS_SUCCESSFUL) { |
---|
84 | rtems_filesystem_global_location_assign( |
---|
85 | &rtems_filesystem_root, |
---|
86 | new_root_loc |
---|
87 | ); |
---|
88 | rtems_filesystem_global_location_assign( |
---|
89 | &rtems_filesystem_current, |
---|
90 | new_current_loc |
---|
91 | ); |
---|
92 | } else { |
---|
93 | if (sc != RTEMS_UNSATISFIED) { |
---|
94 | errno = ENOMEM; |
---|
95 | } |
---|
96 | rv = -1; |
---|
97 | } |
---|
98 | } else { |
---|
99 | rtems_filesystem_location_error( &new_root_loc->location, ENOTDIR ); |
---|
100 | rv = -1; |
---|
101 | } |
---|
102 | |
---|
103 | if ( rv != 0 ) { |
---|
104 | rtems_filesystem_global_location_release( new_root_loc, true ); |
---|
105 | } |
---|
106 | } else { |
---|
107 | rv = -1; |
---|
108 | } |
---|
109 | |
---|
110 | rtems_filesystem_eval_path_cleanup( &ctx ); |
---|
111 | |
---|
112 | if ( rv != 0 ) { |
---|
113 | rtems_filesystem_global_location_release( new_current_loc, false ); |
---|
114 | } |
---|
115 | |
---|
116 | return rv; |
---|
117 | } |
---|