source: rtems/testsuites/libtests/pwdgrp02/init.c @ e02d5dd9

4.115
Last change on this file since e02d5dd9 was e02d5dd9, checked in by Sebastian Huber <sebastian.huber@…>, on 11/17/14 at 12:35:58

Ensure security of default user environment

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <grp.h>
24#include <pwd.h>
25#include <stdio.h>
26#include <unistd.h>
27
28#include "tmacros.h"
29
30const char rtems_test_name[] = "PWDGRP 2";
31
32static void assert_pwd(struct passwd *pwd)
33{
34  rtems_test_assert(strcmp(pwd->pw_name, "root") == 0);
35  rtems_test_assert(strcmp(pwd->pw_passwd, "") == 0);
36  rtems_test_assert(pwd->pw_uid == 0);
37  rtems_test_assert(pwd->pw_gid == 0);
38  rtems_test_assert(strcmp(pwd->pw_comment, "") == 0);
39  rtems_test_assert(strcmp(pwd->pw_gecos, "") == 0);
40  rtems_test_assert(strcmp(pwd->pw_dir, "") == 0);
41  rtems_test_assert(strcmp(pwd->pw_shell, "") == 0);
42}
43
44static void assert_grp(struct group *grp)
45{
46  rtems_test_assert(strcmp(grp->gr_name, "root") == 0);
47  rtems_test_assert(strcmp(grp->gr_passwd, "") == 0);
48  rtems_test_assert(grp->gr_gid == 0);
49  rtems_test_assert(grp->gr_mem[0] == NULL);
50}
51
52static void assert_dir(const char *name)
53{
54  int rv;
55  struct stat st;
56
57  rv = lstat(name, &st);
58  rtems_test_assert(rv == 0);
59  rtems_test_assert(st.st_uid == 0);
60  rtems_test_assert(st.st_gid == 0);
61  rtems_test_assert(
62    st.st_mode == (S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
63  );
64}
65
66static void test(void)
67{
68  int rv;
69  struct passwd pwd;
70  struct group grp;
71  struct passwd *pwd_res;
72  struct group *grp_res;
73  char buf[256];
74
75  rtems_test_assert(getuid() == 0);
76  rtems_test_assert(geteuid() == 0);
77
78  rtems_test_assert(getgid() == 0);
79  rtems_test_assert(getegid() == 0);
80
81  memset(&pwd, 0xff, sizeof(pwd));
82  rv = getpwnam_r("root", &pwd, &buf[0], sizeof(buf), &pwd_res);
83  rtems_test_assert(rv == 0);
84  rtems_test_assert(&pwd == pwd_res);
85  assert_pwd(pwd_res);
86
87  memset(&pwd, 0xff, sizeof(pwd));
88  rv = getpwuid_r(0, &pwd, &buf[0], sizeof(buf), &pwd_res);
89  rtems_test_assert(rv == 0);
90  rtems_test_assert(&pwd == pwd_res);
91  assert_pwd(pwd_res);
92
93  memset(&grp, 0xff, sizeof(grp));
94  rv = getgrnam_r("root", &grp, &buf[0], sizeof(buf), &grp_res);
95  rtems_test_assert(rv == 0);
96  rtems_test_assert(&grp == grp_res);
97  assert_grp(grp_res);
98
99  memset(&grp, 0xff, sizeof(grp));
100  rv = getgrgid_r(0, &grp, &buf[0], sizeof(buf), &grp_res);
101  rtems_test_assert(rv == 0);
102  rtems_test_assert(&grp == grp_res);
103  assert_grp(grp_res);
104
105  assert_dir("/dev");
106  assert_dir("/etc");
107
108  rv = setuid(1);
109  rtems_test_assert(rv == 0);
110
111  rv = seteuid(1);
112  rtems_test_assert(rv == 0);
113
114  errno = 0;
115  rv = unlink("/etc/passwd");
116  rtems_test_assert(rv == -1);
117  rtems_test_assert(errno == EACCES);
118
119  errno = 0;
120  rv = unlink("/etc/group");
121  rtems_test_assert(rv == -1);
122  rtems_test_assert(errno == EACCES);
123
124  errno = 0;
125  rv = open("/etc/passwd", O_RDONLY);
126  rtems_test_assert(rv == -1);
127  rtems_test_assert(errno == EACCES);
128
129  errno = 0;
130  rv = open("/etc/group", O_RDONLY);
131  rtems_test_assert(rv == -1);
132  rtems_test_assert(errno == EACCES);
133
134  errno = 0;
135  rv = open("/etc/passwd", O_WRONLY);
136  rtems_test_assert(rv == -1);
137  rtems_test_assert(errno == EACCES);
138
139  errno = 0;
140  rv = open("/etc/group", O_WRONLY);
141  rtems_test_assert(rv == -1);
142  rtems_test_assert(errno == EACCES);
143
144  errno = 0;
145  rv = open("/etc/passwd", 0);
146  rtems_test_assert(rv == -1);
147  rtems_test_assert(errno == EACCES);
148
149  errno = 0;
150  rv = open("/etc/group", 0);
151  rtems_test_assert(rv == -1);
152  rtems_test_assert(errno == EACCES);
153}
154
155static void Init(rtems_task_argument arg)
156{
157  TEST_BEGIN();
158
159  test();
160
161  TEST_END();
162  rtems_test_exit(0);
163}
164
165#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
166#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
167
168#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
169
170#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
171
172#define CONFIGURE_MAXIMUM_TASKS 1
173
174#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
175
176#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
177
178#define CONFIGURE_INIT
179
180#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.