source: rtems/testsuites/psxtests/psximfs02/init.c @ 9d945b5f

5
Last change on this file since 9d945b5f was 9d945b5f, checked in by Joel Sherrill <joel@…>, on 06/15/17 at 17:42:26

psximfs02/init.c: Avoid potential string overflow

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2015.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <tmacros.h>
15#include "test_support.h"
16
17#include <unistd.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <errno.h>
22#include <rtems/libio.h>
23#include <rtems/malloc.h>
24#include <rtems/libcsupport.h>
25
26const char rtems_test_name[] = "PSXIMFS 2";
27
28#if !HAVE_DECL_SETEUID
29extern int seteuid(uid_t euid);
30#endif
31
32/* forward declarations to avoid warnings */
33rtems_task Init(rtems_task_argument argument);
34
35rtems_task Init(
36  rtems_task_argument argument
37)
38{
39  static const char mount_point [] = "dir01";
40  static const char fs_type [] = RTEMS_FILESYSTEM_TYPE_IMFS;
41  static const char slink_2_name [] = "node-slink-2";
42  static const uintptr_t mount_table_entry_size [] = {
43    sizeof( rtems_filesystem_mount_table_entry_t )
44      + sizeof( fs_type )
45      + sizeof( rtems_filesystem_global_location_t )
46  };
47  static const uintptr_t slink_2_name_size [] = {
48    sizeof( slink_2_name )
49  };
50
51  int status = 0;
52  void *opaque;
53  char linkname_n[20] = {0};
54  char linkname_p[20] = {0};
55  int i;
56  struct stat stat_buf;
57
58  TEST_BEGIN();
59
60  puts( "Creating directory /dir00" );
61  status = mkdir( "/dir00", S_IRWXU );
62  rtems_test_assert( status == 0 );
63
64  puts( "Creating directory /dir00/dir01" );
65  status = mkdir( "/dir00/dir01", S_IRWXU );
66  rtems_test_assert( status == 0 );
67
68  puts( "Changing directory to /dir00" );
69  status = chdir( "/dir00" );
70  rtems_test_assert( status == 0 );
71
72  puts( "Creating link dir01-link0 for dir01" );
73  status = link( "dir01", "dir01-link0" );
74  rtems_test_assert( status == 0 );
75
76  for( i = 1 ; ; ++i ) {
77    sprintf( linkname_p, "dir01-link%04d", i-1 );
78    sprintf( linkname_n, "dir01-link%04d", i );
79    printf( "\nCreating link %s for %s\n", linkname_n, linkname_p );
80    status = link( linkname_p, linkname_n );
81    if( status != 0 ) {
82      puts("Link creation failed" );
83      break;
84    }
85  }
86
87  puts( "Creating a regular node /node, RDONLY" );
88  status = mknod( "/node", S_IFREG | S_IRUSR, 0LL );
89  rtems_test_assert( status == 0 );
90
91  puts( "Creating link /node-link for /node" );
92  status = link( "/node" , "/node-link" );
93  rtems_test_assert( status == 0 );
94
95  puts( "Opening /node-link in WRONLY mode -- expect EACCES" );
96  status = open( "/node-link", O_WRONLY );
97  rtems_test_assert( status == -1 );
98  rtems_test_assert( errno == EACCES );
99
100  puts( "Creating a symlink /node-slink for /node" );
101  status = symlink( "/node" , "/node-slink" );
102  rtems_test_assert( status == 0 );
103
104  puts( "Opening /node-slink in WRONLY mode -- expect EACCES" ); 
105  status = open( "/node-slink", O_WRONLY );
106  rtems_test_assert( status == -1 );
107  rtems_test_assert( errno == EACCES );
108
109  puts( "Allocate most of heap" );
110  opaque = rtems_heap_greedy_allocate( mount_table_entry_size, 1 );
111
112  printf( "Attempt to mount a fs at %s -- expect ENOMEM", mount_point );
113  status = mount( NULL,
114                  mount_point,
115                  fs_type,
116                  RTEMS_FILESYSTEM_READ_WRITE,
117                  NULL );
118  rtems_test_assert( status == -1 );
119  rtems_test_assert( errno == ENOMEM );
120
121  puts( "Freeing allocated memory" );
122  rtems_heap_greedy_free( opaque );
123
124  puts( "Changing directory to /" );
125  status = chdir( "/" );
126  rtems_test_assert( status == 0 );
127
128  puts( "Allocate most of heap" );
129  opaque = rtems_heap_greedy_allocate( NULL, 0 );
130
131  puts( "Attempt to create /node-link-2 for /node -- expect ENOMEM" );
132  status = link( "/node", "/node-link-2" );
133  rtems_test_assert( status == -1 );
134  rtems_test_assert( errno == ENOMEM );
135
136  puts( "Attempt to create /node-slink-2 for /node -- expect ENOMEM" );
137  status = symlink( "/node", "node-slink-2" );
138  rtems_test_assert( status == -1 );
139  rtems_test_assert( errno == ENOMEM );
140
141  puts( "Freeing allocated memory" );
142  rtems_heap_greedy_free( opaque );
143
144  puts( "Allocate most of heap" );
145  opaque = rtems_heap_greedy_allocate( slink_2_name_size, 1 );
146
147  printf( "Attempt to create %s for /node -- expect ENOMEM", slink_2_name );
148  status = symlink( "/node", slink_2_name );
149  rtems_test_assert( status == -1 );
150  rtems_test_assert( errno == ENOMEM );
151
152  puts( "Freeing allocated memory" );
153  rtems_heap_greedy_free( opaque );
154
155  puts( "Attempt to stat a hardlink" );
156  status = lstat( "/node-link", &stat_buf );
157  rtems_test_assert( status == 0 );
158
159  puts( "Changing euid to 10" );
160  status = seteuid( 10 );
161  rtems_test_assert( status == 0 );
162
163  puts( "Attempt chmod on /node -- expect EPERM" );
164  status = chmod( "/node", S_IRUSR );
165  rtems_test_assert( status == -1 );
166  rtems_test_assert( errno == EPERM );
167
168  puts( "Attempt chown on /node -- expect EPERM" );
169  status = chown( "/node", 10, 10 );
170  rtems_test_assert( status == -1 );
171  rtems_test_assert( errno == EPERM );
172
173  puts( "Changing euid back to 0 [root]" );
174  status = seteuid( 0 );
175  rtems_test_assert( status == 0 );
176
177  puts( "Creating a fifo -- OK" );
178  status = mkfifo( "/fifo", S_IRWXU );
179  rtems_test_assert( status == 0 );
180 
181  puts( "chown /fifo to 10 -- OK" );
182  status = chown( "/fifo", 10, 10 );
183  rtems_test_assert( status == 0 );
184
185  puts( "Changing euid to 10" );
186  status = seteuid( 10 );
187  rtems_test_assert( status == 0 );
188
189  puts( "chmod /fifo -- OK" );
190  status = chmod( "/fifo", S_IRWXU );
191  rtems_test_assert( status == 0 );
192
193  printf( "chown /fifo to %o -- OK\n", 0 );
194  status = chown( "/fifo", 0, 0 );
195  rtems_test_assert( status == 0 );
196
197  TEST_END();
198  rtems_test_exit(0);
199}
200
201/* configuration information */
202
203#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
204#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
205
206#define CONFIGURE_FILESYSTEM_IMFS
207
208#define CONFIGURE_MAXIMUM_TASKS                  1
209#define CONFIGURE_IMFS_MEMFILE_BYTES_PER_BLOCK   16
210#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
211#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
212
213#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
214
215#define CONFIGURE_INIT
216
217#define CONFIGURE_MAXIMUM_FIFOS 1
218
219#include <rtems/confdefs.h>
220/* end of file */
Note: See TracBrowser for help on using the repository browser.