source: rtems/cpukit/libfs/src/imfs/imfs_eval.c @ ec9d486

4.10
Last change on this file since ec9d486 was ec9d486, checked in by Chris Johns <chrisj@…>, on 04/16/11 at 03:47:32

2011-04-16 Chris Johns <chrisj@…>

PR 1774/filesystem

  • libfs/src/imfs/imfs_eval.c: Fix the IMFS eval and eval for make handlers to not inspect a mounted file sytems path.
  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 *  Evaluation IMFS Node Support Routines
3 *
4 *  COPYRIGHT (c) 1989-1999.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <unistd.h>
22#include <errno.h>
23#include <stdlib.h>
24#include <assert.h>
25
26#include "imfs.h"
27#include <rtems/libio_.h>
28#include <rtems/seterr.h>
29
30#define RTEMS_LIBIO_PERMS_RX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_READ)
31#define RTEMS_LIBIO_PERMS_WX (RTEMS_LIBIO_PERMS_SEARCH | RTEMS_LIBIO_PERMS_WRITE)
32
33#define MAXSYMLINK 5
34
35int IMFS_Set_handlers(
36  rtems_filesystem_location_info_t   *loc
37)
38{
39  IMFS_jnode_t    *node = loc->node_access;
40  IMFS_fs_info_t  *fs_info;
41
42  fs_info = loc->mt_entry->fs_info;
43  switch( node->type ) {
44    case IMFS_DIRECTORY:
45      loc->handlers = fs_info->directory_handlers;
46      break;
47    case IMFS_DEVICE:
48      loc->handlers = &IMFS_device_handlers;
49      break;
50    case IMFS_SYM_LINK:
51    case IMFS_HARD_LINK:
52      loc->handlers = &IMFS_link_handlers;
53      break;
54    case IMFS_LINEAR_FILE:
55      loc->handlers = fs_info->memfile_handlers;
56      break;
57    case IMFS_MEMORY_FILE:
58      loc->handlers = fs_info->memfile_handlers;
59      break;
60    case IMFS_FIFO:
61      loc->handlers = &IMFS_fifo_handlers;
62      break;
63  }
64
65  return 0;
66}
67
68/*
69 *  IMFS_evaluate_permission
70 *
71 *  The following routine evaluates that we have permission
72 *  to do flags on the node.
73 */
74
75int IMFS_evaluate_permission(
76  rtems_filesystem_location_info_t  *node,
77  int                                flags
78)
79{
80  uid_t         st_uid;
81  gid_t         st_gid;
82  IMFS_jnode_t *jnode;
83  int           flags_to_test;
84
85  if ( !rtems_libio_is_valid_perms( flags ) )
86    rtems_set_errno_and_return_minus_one( EPERM );
87
88  jnode = node->node_access;
89
90#if defined(RTEMS_POSIX_API)
91  st_uid = geteuid();
92  st_gid = getegid();
93#else
94  st_uid = jnode->st_uid;
95  st_gid = jnode->st_gid;
96#endif
97
98  /*
99   * Check if I am owner or a group member or someone else.
100   */
101
102  flags_to_test = flags;
103
104  if ( st_uid == jnode->st_uid )
105    flags_to_test <<= 6;
106  else if ( st_gid == jnode->st_gid )
107    flags_to_test <<= 3;
108  else {
109    /* must be other - do nothing */;
110  }
111
112  /*
113   * If all of the flags are set we have permission
114   * to do this.
115   */
116  if ( ( flags_to_test & jnode->st_mode) == flags_to_test )
117    return 1;
118
119  return 0;
120}
121
122/*
123 *  IMFS_evaluate_hard_link
124 *
125 *  The following routine evaluates a hardlink to the actual node.
126 */
127
128int IMFS_evaluate_hard_link(
129  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
130  int                                flags   /* IN     */
131)
132{
133  IMFS_jnode_t                     *jnode  = node->node_access;
134  int                               result = 0;
135
136  /*
137   * Check for things that should never happen.
138   */
139
140  if ( jnode->type != IMFS_HARD_LINK )
141    rtems_fatal_error_occurred (0xABCD0000);
142
143  /*
144   * Set the hard link value and the handlers.
145   */
146
147  node->node_access = jnode->info.hard_link.link_node;
148
149  IMFS_Set_handlers( node );
150
151  /*
152   * Verify we have the correct permissions for this node.
153   */
154
155  if ( !IMFS_evaluate_permission( node, flags ) )
156    rtems_set_errno_and_return_minus_one( EACCES );
157
158  return result;
159}
160
161
162/*
163 *  IMFS_evaluate_sym_link
164 *
165 *  The following routine evaluates a symbolic link to the actual node.
166 */
167
168int IMFS_evaluate_sym_link(
169  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
170  int                                flags   /* IN     */
171)
172{
173  IMFS_jnode_t                     *jnode  = node->node_access;
174  int                               result = 0;
175  int                               i;
176
177  /*
178   * Check for things that should never happen.
179   */
180
181  if ( jnode->type != IMFS_SYM_LINK )
182    rtems_fatal_error_occurred (0xABCD0000);
183
184  if ( !jnode->Parent )
185    rtems_fatal_error_occurred( 0xBAD00000 );
186
187
188  /*
189   * Move the node_access to either the symbolic links parent or
190   * root depending on the symbolic links path.
191   */
192
193  node->node_access = jnode->Parent;
194
195  rtems_filesystem_get_sym_start_loc(
196    jnode->info.sym_link.name,
197    &i,
198    node
199  );
200
201  /*
202   * Use eval path to evaluate the path of the symbolic link.
203   */
204
205  result = IMFS_eval_path(
206    &jnode->info.sym_link.name[i],
207    strlen( &jnode->info.sym_link.name[i] ),
208    flags,
209    node
210  );
211
212  IMFS_Set_handlers( node );
213
214  /*
215   * Verify we have the correct permissions for this node.
216   */
217
218  if ( !IMFS_evaluate_permission( node, flags ) )
219    rtems_set_errno_and_return_minus_one( EACCES );
220
221  return result;
222}
223
224/*
225 *  IMFS_evaluate_link
226 *
227 *  The following routine returns the real node pointed to by a link.
228 */
229
230int IMFS_evaluate_link(
231  rtems_filesystem_location_info_t  *node,   /* IN/OUT */
232  int                                flags   /* IN     */
233)
234{
235  IMFS_jnode_t                     *jnode;
236  int                               result = 0;
237
238  do {
239    jnode  = node->node_access;
240
241    /*
242     * Increment and check the link counter.
243     */
244
245    rtems_filesystem_link_counts ++;
246    if ( rtems_filesystem_link_counts > MAXSYMLINK ) {
247      rtems_filesystem_link_counts = 0;
248      rtems_set_errno_and_return_minus_one( ELOOP );
249    }
250
251    /*
252     *  Follow the Link node.
253     */
254
255    if ( jnode->type == IMFS_HARD_LINK )
256      result = IMFS_evaluate_hard_link( node, flags );
257
258    else if (jnode->type == IMFS_SYM_LINK )
259      result = IMFS_evaluate_sym_link( node, flags );
260
261  } while ( ( result == 0 ) && ( ( jnode->type == IMFS_SYM_LINK  ) ||
262                                 ( jnode->type == IMFS_HARD_LINK ) ) );
263
264  /*
265   * Clear link counter.
266   */
267
268  rtems_filesystem_link_counts = 0;
269
270  return result;
271}
272
273
274/*
275 *  IMFS_evaluate_for_make
276 *
277 *  The following routine evaluate path for a new node to be created.
278 *  pathloc is returned with a pointer to the parent of the new node.
279 *  name is returned with a pointer to the first character in the
280 *  new node name.  The parent node is verified to be a directory.
281 */
282
283int IMFS_evaluate_for_make(
284   const char                         *path,       /* IN     */
285   rtems_filesystem_location_info_t   *pathloc,    /* IN/OUT */
286   const char                        **name        /* OUT    */
287)
288{
289  int                                 i = 0;
290  int                                 len;
291  IMFS_token_types                    type;
292  char                                token[ IMFS_NAME_MAX + 1 ];
293  rtems_filesystem_location_info_t    newloc;
294  IMFS_jnode_t                       *node;
295  bool                                done = false;
296  int                                 pathlen;
297  int                                 result;
298
299  /*
300   * This was filled in by the caller and is valid in the
301   * mount table.
302   */
303  node = pathloc->node_access;
304
305  /*
306   * Get the path length.
307   */
308  pathlen = strlen( path );
309
310  /*
311   *  Evaluate all tokens until we are done or an error occurs.
312   */
313
314  while( !done ) {
315
316    type = IMFS_get_token( &path[i], pathlen, token, &len );
317    pathlen -= len;
318    i +=  len;
319
320    if ( !pathloc->node_access )
321      rtems_set_errno_and_return_minus_one( ENOENT );
322
323    /*
324     * I cannot move out of this directory without execute permission.
325     */
326
327    if ( type != IMFS_NO_MORE_PATH )
328      if ( node->type == IMFS_DIRECTORY )
329        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
330           rtems_set_errno_and_return_minus_one( EACCES );
331
332    node = pathloc->node_access;
333
334    switch( type ) {
335
336      case IMFS_UP_DIR:
337       /*
338        *  Am I at the root of all filesystems? (chroot'ed?)
339        */
340
341       if ( pathloc->node_access == rtems_filesystem_root.node_access )
342         break;       /* Throw out the .. in this case */
343
344
345       /*
346        * Am I at the root of this mounted filesystem?
347        */
348
349        if (pathloc->node_access ==
350            pathloc->mt_entry->mt_fs_root.node_access){
351
352          /*
353           *  Am I at the root of all filesystems?
354           */
355
356          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
357            break;
358          } else {
359            newloc = pathloc->mt_entry->mt_point_node;
360            *pathloc = newloc;
361            return (*pathloc->ops->evalformake_h)( &path[i], pathloc, name );
362          }
363        } else {
364          if ( !node->Parent )
365            rtems_set_errno_and_return_minus_one( ENOENT );
366
367          node = node->Parent;
368        }
369
370        pathloc->node_access = node;
371        break;
372
373      case IMFS_NAME:
374        /*
375         *  If we are at a link follow it.
376         */
377
378        if ( node->type == IMFS_HARD_LINK ) {
379
380          result = IMFS_evaluate_link( pathloc, 0 );
381          if ( result == -1 )
382            return -1;
383
384        } else if ( node->type == IMFS_SYM_LINK ) {
385
386          result = IMFS_evaluate_link( pathloc, 0 );
387
388          if ( result == -1 )
389            return -1;
390        }
391
392        node = pathloc->node_access;
393        if ( !node )
394          rtems_set_errno_and_return_minus_one( ENOTDIR );
395
396        /*
397         * Only a directory can be decended into.
398         */
399
400        if ( node->type != IMFS_DIRECTORY )
401          rtems_set_errno_and_return_minus_one( ENOTDIR );
402
403        /*
404         * Otherwise find the token name in the present location.
405         */
406
407        node = IMFS_find_match_in_dir( node, token );
408
409        /*
410         * If there is no node we have found the name of the node we
411         * wish to create.
412         */
413
414        if ( ! node )
415          done = true;
416        else {
417          /*
418           * If we are at a node that is a mount point. Set loc to the
419           * new fs root node and let them finish evaluating the path.
420           */
421
422          if ( node->info.directory.mt_fs != NULL ) {
423            newloc  = node->info.directory.mt_fs->mt_fs_root;
424            *pathloc = newloc;
425            return (*pathloc->ops->evalformake_h)( &path[i], pathloc, name );
426          }
427         
428          pathloc->node_access = node;
429        }
430        break;
431
432      case IMFS_NO_MORE_PATH:
433        rtems_set_errno_and_return_minus_one( EEXIST );
434        break;
435
436      case IMFS_INVALID_TOKEN:
437        rtems_set_errno_and_return_minus_one( ENAMETOOLONG );
438        break;
439
440      case IMFS_CURRENT_DIR:
441        break;
442    }
443  }
444
445  *name = &path[ i - len ];
446
447  /*
448   * We have evaluated the path as far as we can.
449   * Verify there is not any invalid stuff at the end of the name.
450   */
451
452  for( ; path[i] != '\0'; i++) {
453    if ( !IMFS_is_separator( path[ i ] ) )
454      rtems_set_errno_and_return_minus_one( ENOENT );
455  }
456
457  /*
458   * Verify we can execute and write to this directory.
459   */
460
461  result = IMFS_Set_handlers( pathloc );
462
463  /*
464   * The returned node must be a directory
465   */
466  node = pathloc->node_access;
467  if ( node->type != IMFS_DIRECTORY )
468    rtems_set_errno_and_return_minus_one( ENOTDIR );
469
470  /*
471   * We must have Write and execute permission on the returned node.
472   */
473
474  if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_WX ) )
475    rtems_set_errno_and_return_minus_one( EACCES );
476
477  return result;
478}
479
480
481/*
482 *  IMFS_eval_path
483 *
484 *  The following routine evaluate path for a node that wishes to be
485 *  accessed with mode.  pathloc is returned with a pointer to the
486 *  node to be accessed.
487 */
488
489int IMFS_eval_path(
490  const char                        *pathname,     /* IN     */
491  size_t                             pathnamelen,  /* IN     */
492  int                                flags,        /* IN     */
493  rtems_filesystem_location_info_t  *pathloc       /* IN/OUT */
494                   )
495{
496  int                                 i = 0;
497  int                                 len;
498  IMFS_token_types                    type = IMFS_CURRENT_DIR;
499  char                                token[ IMFS_NAME_MAX + 1 ];
500  rtems_filesystem_location_info_t    newloc;
501  IMFS_jnode_t                       *node;
502  int                                 result;
503
504  if ( !rtems_libio_is_valid_perms( flags ) ) {
505    assert( 0 );
506    rtems_set_errno_and_return_minus_one( EIO );
507  }
508
509  /*
510   *  This was filled in by the caller and is valid in the
511   *  mount table.
512   */
513
514  node = pathloc->node_access;
515
516  /*
517   *  Evaluate all tokens until we are done or an error occurs.
518   */
519
520  while( (type != IMFS_NO_MORE_PATH) && (type != IMFS_INVALID_TOKEN) ) {
521
522    type = IMFS_get_token( &pathname[i], pathnamelen, token, &len );
523    pathnamelen -= len;
524    i += len;
525
526    if ( !pathloc->node_access )
527      rtems_set_errno_and_return_minus_one( ENOENT );
528
529    /*
530     * I cannot move out of this directory without execute permission.
531     */
532    if ( type != IMFS_NO_MORE_PATH )
533      if ( node->type == IMFS_DIRECTORY )
534        if ( !IMFS_evaluate_permission( pathloc, RTEMS_LIBIO_PERMS_SEARCH ) )
535          rtems_set_errno_and_return_minus_one( EACCES );
536
537    node = pathloc->node_access;
538
539    switch( type ) {
540      case IMFS_UP_DIR:
541        /*
542         *  Am I at the root of all filesystems? (chroot'ed?)
543         */
544
545        if ( pathloc->node_access == rtems_filesystem_root.node_access )
546          break;       /* Throw out the .. in this case */
547
548        /*
549         *  Am I at the root of this mounted filesystem?
550         */
551
552        if (pathloc->node_access ==
553            pathloc->mt_entry->mt_fs_root.node_access) {
554
555          /*
556           *  Am I at the root of all filesystems?
557           */
558
559          if ( pathloc->node_access == rtems_filesystem_root.node_access ) {
560            break;       /* Throw out the .. in this case */
561          } else {
562            newloc = pathloc->mt_entry->mt_point_node;
563            *pathloc = newloc;
564            return (*pathloc->ops->evalpath_h)(&(pathname[i]),
565                                               pathnamelen,
566                                               flags,pathloc);
567          }
568        } else {
569          if ( !node->Parent )
570            rtems_set_errno_and_return_minus_one( ENOENT );
571
572          node = node->Parent;
573        }
574
575        pathloc->node_access = node;
576        break;
577
578      case IMFS_NAME:
579        /*
580         *  If we are at a link follow it.
581         */
582
583        if ( node->type == IMFS_HARD_LINK ) {
584
585          IMFS_evaluate_hard_link( pathloc, 0 );
586
587          node = pathloc->node_access;
588          if ( !node )
589            rtems_set_errno_and_return_minus_one( ENOTDIR );
590
591        } else if ( node->type == IMFS_SYM_LINK ) {
592
593          result = IMFS_evaluate_sym_link( pathloc, 0 );
594
595          node = pathloc->node_access;
596          if ( result == -1 )
597            return -1;
598        }
599
600        /*
601         *  Only a directory can be decended into.
602         */
603
604        if ( node->type != IMFS_DIRECTORY )
605          rtems_set_errno_and_return_minus_one( ENOTDIR );
606
607        /*
608         *  Otherwise find the token name in the present location.
609         */
610
611        node = IMFS_find_match_in_dir( node, token );
612
613        if ( !node )
614          rtems_set_errno_and_return_minus_one( ENOENT );
615
616
617        /*
618         *  Set the node access to the point we have found.
619         */
620
621        pathloc->node_access = node;
622       
623        /*
624         *  If we are at a node that is a mount point. Set loc to the
625         *  new fs root node and let them finish evaluating the path.
626         */
627
628        if ( node->info.directory.mt_fs != NULL ) {
629          newloc   = node->info.directory.mt_fs->mt_fs_root;
630          *pathloc = newloc;
631          return (*pathloc->ops->evalpath_h)( &pathname[i],
632                                              pathnamelen,
633                                              flags, pathloc );
634        }
635        break;
636
637      case IMFS_NO_MORE_PATH:
638      case IMFS_CURRENT_DIR:
639        break;
640
641      case IMFS_INVALID_TOKEN:
642        rtems_set_errno_and_return_minus_one( ENAMETOOLONG );
643        break;
644
645    }
646  }
647
648  /*
649   *  Always return the root node.
650   *
651   *  If we are at a node that is a mount point. Set loc to the
652   *  new fs root node and let let the mounted filesystem set the handlers.
653   *
654   *  NOTE: The behavior of stat() on a mount point appears to be questionable.
655   */
656
657  if ( node->type == IMFS_DIRECTORY ) {
658    if ( node->info.directory.mt_fs != NULL ) {
659      newloc   = node->info.directory.mt_fs->mt_fs_root;
660      *pathloc = newloc;
661      return (*pathloc->ops->evalpath_h)( &pathname[i-len],
662                                          pathnamelen+len,
663                                          flags, pathloc );
664    } else {
665      result = IMFS_Set_handlers( pathloc );
666    }
667  } else {
668    result = IMFS_Set_handlers( pathloc );
669  }
670
671  /*
672   * Verify we have the correct permissions for this node.
673   */
674
675  if ( !IMFS_evaluate_permission( pathloc, flags ) )
676    rtems_set_errno_and_return_minus_one( EACCES );
677
678  return result;
679}
Note: See TracBrowser for help on using the repository browser.