pam_namespace.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /******************************************************************************
  2. * A module for Linux-PAM that will set the default namespace after
  3. * establishing a session via PAM.
  4. *
  5. * (C) Copyright IBM Corporation 2005
  6. * (C) Copyright Red Hat 2006
  7. * All Rights Reserved.
  8. *
  9. * Written by: Janak Desai <janak@us.ibm.com>
  10. * With Revisions by: Steve Grubb <sgrubb@redhat.com>
  11. * Derived from a namespace setup patch by Chad Sellers <cdselle@tycho.nsa.gov>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * on the rights to use, copy, modify, merge, publish, distribute, sub
  17. * license, and/or sell copies of the Software, and to permit persons to whom
  18. * the Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  27. * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #if !(defined(linux))
  33. #error THIS CODE IS KNOWN TO WORK ONLY ON LINUX !!!
  34. #endif
  35. #include "config.h"
  36. #include <stdio.h>
  37. #include <stdio_ext.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include <syslog.h>
  44. #include <dlfcn.h>
  45. #include <stdarg.h>
  46. #include <pwd.h>
  47. #include <grp.h>
  48. #include <limits.h>
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <sys/resource.h>
  52. #include <sys/mount.h>
  53. #include <sys/wait.h>
  54. #include <libgen.h>
  55. #include <fcntl.h>
  56. #include <sched.h>
  57. #include <glob.h>
  58. #include <locale.h>
  59. #include "security/pam_modules.h"
  60. #include "security/pam_modutil.h"
  61. #include "security/pam_ext.h"
  62. #include "md5.h"
  63. #ifdef WITH_SELINUX
  64. #include <selinux/selinux.h>
  65. #include <selinux/get_context_list.h>
  66. #include <selinux/context.h>
  67. #include <selinux/label.h>
  68. #endif
  69. #ifndef CLONE_NEWNS
  70. #define CLONE_NEWNS 0x00020000 /* Flag to create new namespace */
  71. #endif
  72. /* mount flags for mount_private */
  73. #ifndef MS_REC
  74. #define MS_REC (1<<14)
  75. #endif
  76. #ifndef MS_PRIVATE
  77. #define MS_PRIVATE (1<<18)
  78. #endif
  79. #ifndef MS_SLAVE
  80. #define MS_SLAVE (1<<19)
  81. #endif
  82. /*
  83. * Module defines
  84. */
  85. #ifndef SECURECONF_DIR
  86. #define SECURECONF_DIR "/etc/security/"
  87. #endif
  88. #define PAM_NAMESPACE_CONFIG (SECURECONF_DIR "namespace.conf")
  89. #define NAMESPACE_INIT_SCRIPT (SECURECONF_DIR "namespace.init")
  90. #define NAMESPACE_D_DIR (SECURECONF_DIR "namespace.d/")
  91. #define NAMESPACE_D_GLOB (SECURECONF_DIR "namespace.d/*.conf")
  92. /* module flags */
  93. #define PAMNS_DEBUG 0x00000100 /* Running in debug mode */
  94. #define PAMNS_SELINUX_ENABLED 0x00000400 /* SELinux is enabled */
  95. #define PAMNS_CTXT_BASED_INST 0x00000800 /* Context based instance needed */
  96. #define PAMNS_GEN_HASH 0x00002000 /* Generate md5 hash for inst names */
  97. #define PAMNS_IGN_CONFIG_ERR 0x00004000 /* Ignore format error in conf file */
  98. #define PAMNS_IGN_INST_PARENT_MODE 0x00008000 /* Ignore instance parent mode */
  99. #define PAMNS_UNMOUNT_ON_CLOSE 0x00010000 /* Unmount at session close */
  100. #define PAMNS_USE_CURRENT_CONTEXT 0x00020000 /* use getcon instead of getexeccon */
  101. #define PAMNS_USE_DEFAULT_CONTEXT 0x00040000 /* use get_default_context instead of getexeccon */
  102. #define PAMNS_MOUNT_PRIVATE 0x00080000 /* Make the polydir mounts private */
  103. /* polydir flags */
  104. #define POLYDIR_EXCLUSIVE 0x00000001 /* polyinstatiate exclusively for override uids */
  105. #define POLYDIR_CREATE 0x00000002 /* create the polydir */
  106. #define POLYDIR_NOINIT 0x00000004 /* no init script */
  107. #define POLYDIR_SHARED 0x00000008 /* share context/level instances among users */
  108. #define POLYDIR_ISCRIPT 0x00000010 /* non default init script */
  109. #define POLYDIR_MNTOPTS 0x00000020 /* mount options for tmpfs mount */
  110. #define NAMESPACE_MAX_DIR_LEN 80
  111. #define NAMESPACE_POLYDIR_DATA "pam_namespace:polydir_data"
  112. #define NAMESPACE_PROTECT_DATA "pam_namespace:protect_data"
  113. /*
  114. * Polyinstantiation method options, based on user, security context
  115. * or both
  116. */
  117. enum polymethod {
  118. NONE,
  119. USER,
  120. CONTEXT,
  121. LEVEL,
  122. TMPDIR,
  123. TMPFS
  124. };
  125. /*
  126. * Depending on the application using this namespace module, we
  127. * may need to unmount previously bind mounted instance directory.
  128. * Applications such as login and sshd, that establish a new
  129. * session unmount of instance directory is not needed. For applications
  130. * such as su and newrole, that switch the identity, this module
  131. * has to unmount previous instance directory first and re-mount
  132. * based on the new identity. For other trusted applications that
  133. * just want to undo polyinstantiation, only unmount of previous
  134. * instance directory is needed.
  135. */
  136. enum unmnt_op {
  137. NO_UNMNT,
  138. UNMNT_REMNT,
  139. UNMNT_ONLY,
  140. };
  141. /*
  142. * Structure that holds information about a directory to polyinstantiate
  143. */
  144. struct polydir_s {
  145. char dir[PATH_MAX]; /* directory to polyinstantiate */
  146. char rdir[PATH_MAX]; /* directory to unmount (based on RUSER) */
  147. char instance_prefix[PATH_MAX]; /* prefix for instance dir path name */
  148. enum polymethod method; /* method used to polyinstantiate */
  149. unsigned int num_uids; /* number of override uids */
  150. uid_t *uid; /* list of override uids */
  151. unsigned int flags; /* polydir flags */
  152. char *init_script; /* path to init script */
  153. char *mount_opts; /* mount options for tmpfs mount */
  154. unsigned long mount_flags; /* mount flags for tmpfs mount */
  155. uid_t owner; /* user which should own the polydir */
  156. gid_t group; /* group which should own the polydir */
  157. mode_t mode; /* mode of the polydir */
  158. struct polydir_s *next; /* pointer to the next polydir entry */
  159. };
  160. struct protect_dir_s {
  161. char *dir; /* protected directory */
  162. struct protect_dir_s *next; /* next entry */
  163. };
  164. struct instance_data {
  165. pam_handle_t *pamh; /* The pam handle for this instance */
  166. struct polydir_s *polydirs_ptr; /* The linked list pointer */
  167. struct protect_dir_s *protect_dirs; /* The pointer to stack of mount-protected dirs */
  168. char user[LOGIN_NAME_MAX]; /* User name */
  169. char ruser[LOGIN_NAME_MAX]; /* Requesting user name */
  170. uid_t uid; /* The uid of the user */
  171. gid_t gid; /* The gid of the user's primary group */
  172. uid_t ruid; /* The uid of the requesting user */
  173. unsigned long flags; /* Flags for debug, selinux etc */
  174. };