Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:34

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2014 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <sys/stat.h>
0033 #include <sys/types.h>
0034 #include <errno.h>
0035 #include <fcntl.h>
0036 #include <grp.h>
0037 #include <pwd.h>
0038 #include <stdio.h>
0039 #include <unistd.h>
0040 
0041 #include "tmacros.h"
0042 
0043 const char rtems_test_name[] = "PWDGRP 2";
0044 
0045 static void assert_pwd(struct passwd *pwd)
0046 {
0047   rtems_test_assert(strcmp(pwd->pw_name, "root") == 0);
0048   rtems_test_assert(strcmp(pwd->pw_passwd, "") == 0);
0049   rtems_test_assert(pwd->pw_uid == 0);
0050   rtems_test_assert(pwd->pw_gid == 0);
0051   rtems_test_assert(strcmp(pwd->pw_comment, "") == 0);
0052   rtems_test_assert(strcmp(pwd->pw_gecos, "") == 0);
0053   rtems_test_assert(strcmp(pwd->pw_dir, "") == 0);
0054   rtems_test_assert(strcmp(pwd->pw_shell, "") == 0);
0055 }
0056 
0057 static void assert_grp(struct group *grp)
0058 {
0059   rtems_test_assert(strcmp(grp->gr_name, "root") == 0);
0060   rtems_test_assert(strcmp(grp->gr_passwd, "") == 0);
0061   rtems_test_assert(grp->gr_gid == 0);
0062   rtems_test_assert(grp->gr_mem[0] == NULL);
0063 }
0064 
0065 static void assert_dir(const char *name)
0066 {
0067   int rv;
0068   struct stat st;
0069 
0070   rv = lstat(name, &st);
0071   rtems_test_assert(rv == 0);
0072   rtems_test_assert(st.st_uid == 0);
0073   rtems_test_assert(st.st_gid == 0);
0074   rtems_test_assert(
0075     st.st_mode == (S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
0076   );
0077 }
0078 
0079 static void test(void)
0080 {
0081   int rv;
0082   struct passwd pwd;
0083   struct group grp;
0084   struct passwd *pwd_res;
0085   struct group *grp_res;
0086   char buf[256];
0087 
0088   rtems_test_assert(getuid() == 0);
0089   rtems_test_assert(geteuid() == 0);
0090 
0091   rtems_test_assert(getgid() == 0);
0092   rtems_test_assert(getegid() == 0);
0093 
0094   memset(&pwd, 0xff, sizeof(pwd));
0095   rv = getpwnam_r("root", &pwd, &buf[0], sizeof(buf), &pwd_res);
0096   rtems_test_assert(rv == 0);
0097   rtems_test_assert(&pwd == pwd_res);
0098   assert_pwd(pwd_res);
0099 
0100   memset(&pwd, 0xff, sizeof(pwd));
0101   rv = getpwuid_r(0, &pwd, &buf[0], sizeof(buf), &pwd_res);
0102   rtems_test_assert(rv == 0);
0103   rtems_test_assert(&pwd == pwd_res);
0104   assert_pwd(pwd_res);
0105 
0106   memset(&grp, 0xff, sizeof(grp));
0107   rv = getgrnam_r("root", &grp, &buf[0], sizeof(buf), &grp_res);
0108   rtems_test_assert(rv == 0);
0109   rtems_test_assert(&grp == grp_res);
0110   assert_grp(grp_res);
0111 
0112   memset(&grp, 0xff, sizeof(grp));
0113   rv = getgrgid_r(0, &grp, &buf[0], sizeof(buf), &grp_res);
0114   rtems_test_assert(rv == 0);
0115   rtems_test_assert(&grp == grp_res);
0116   assert_grp(grp_res);
0117 
0118   assert_dir("/dev");
0119   assert_dir("/etc");
0120 
0121   rv = setuid(1);
0122   rtems_test_assert(rv == 0);
0123 
0124   rv = seteuid(1);
0125   rtems_test_assert(rv == 0);
0126 
0127   errno = 0;
0128   rv = unlink("/etc/passwd");
0129   rtems_test_assert(rv == -1);
0130   rtems_test_assert(errno == EACCES);
0131 
0132   errno = 0;
0133   rv = unlink("/etc/group");
0134   rtems_test_assert(rv == -1);
0135   rtems_test_assert(errno == EACCES);
0136 
0137   errno = 0;
0138   rv = open("/etc/passwd", O_RDONLY);
0139   rtems_test_assert(rv == -1);
0140   rtems_test_assert(errno == EACCES);
0141 
0142   errno = 0;
0143   rv = open("/etc/group", O_RDONLY);
0144   rtems_test_assert(rv == -1);
0145   rtems_test_assert(errno == EACCES);
0146 
0147   errno = 0;
0148   rv = open("/etc/passwd", O_WRONLY);
0149   rtems_test_assert(rv == -1);
0150   rtems_test_assert(errno == EACCES);
0151 
0152   errno = 0;
0153   rv = open("/etc/group", O_WRONLY);
0154   rtems_test_assert(rv == -1);
0155   rtems_test_assert(errno == EACCES);
0156 
0157   errno = 0;
0158   rv = open("/etc/passwd", 0);
0159   rtems_test_assert(rv == -1);
0160   rtems_test_assert(errno == EACCES);
0161 
0162   errno = 0;
0163   rv = open("/etc/group", 0);
0164   rtems_test_assert(rv == -1);
0165   rtems_test_assert(errno == EACCES);
0166 }
0167 
0168 static void Init(rtems_task_argument arg)
0169 {
0170   TEST_BEGIN();
0171 
0172   test();
0173 
0174   TEST_END();
0175   rtems_test_exit(0);
0176 }
0177 
0178 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0179 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0180 
0181 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0182 
0183 #define CONFIGURE_MAXIMUM_TASKS 1
0184 
0185 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0186 
0187 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0188 
0189 #define CONFIGURE_INIT
0190 
0191 #include <rtems/confdefs.h>