port-solaris.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2006 Chad Mynhier.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "config.h"
  17. #include "includes.h"
  18. #ifdef USE_SOLARIS_PROCESS_CONTRACTS
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/param.h>
  22. #include <errno.h>
  23. #ifdef HAVE_FCNTL_H
  24. # include <fcntl.h>
  25. #endif
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <libcontract.h>
  30. #include <sys/contract/process.h>
  31. #include <sys/ctfs.h>
  32. #include "log.h"
  33. #define CT_TEMPLATE CTFS_ROOT "/process/template"
  34. #define CT_LATEST CTFS_ROOT "/process/latest"
  35. static int tmpl_fd = -1;
  36. /* Lookup the latest process contract */
  37. static ctid_t
  38. get_active_process_contract_id(void)
  39. {
  40. int stat_fd;
  41. ctid_t ctid = -1;
  42. ct_stathdl_t stathdl;
  43. if ((stat_fd = open64(CT_LATEST, O_RDONLY)) == -1) {
  44. error("%s: Error opening 'latest' process "
  45. "contract: %s", __func__, strerror(errno));
  46. return -1;
  47. }
  48. if (ct_status_read(stat_fd, CTD_COMMON, &stathdl) != 0) {
  49. error("%s: Error reading process contract "
  50. "status: %s", __func__, strerror(errno));
  51. goto out;
  52. }
  53. if ((ctid = ct_status_get_id(stathdl)) < 0) {
  54. error("%s: Error getting process contract id: %s",
  55. __func__, strerror(errno));
  56. goto out;
  57. }
  58. ct_status_free(stathdl);
  59. out:
  60. close(stat_fd);
  61. return ctid;
  62. }
  63. void
  64. solaris_contract_pre_fork(void)
  65. {
  66. if ((tmpl_fd = open64(CT_TEMPLATE, O_RDWR)) == -1) {
  67. error("%s: open %s: %s", __func__,
  68. CT_TEMPLATE, strerror(errno));
  69. return;
  70. }
  71. debug2("%s: setting up process contract template on fd %d",
  72. __func__, tmpl_fd);
  73. /* First we set the template parameters and event sets. */
  74. if (ct_pr_tmpl_set_param(tmpl_fd, CT_PR_PGRPONLY) != 0) {
  75. error("%s: Error setting process contract parameter set "
  76. "(pgrponly): %s", __func__, strerror(errno));
  77. goto fail;
  78. }
  79. if (ct_pr_tmpl_set_fatal(tmpl_fd, CT_PR_EV_HWERR) != 0) {
  80. error("%s: Error setting process contract template "
  81. "fatal events: %s", __func__, strerror(errno));
  82. goto fail;
  83. }
  84. if (ct_tmpl_set_critical(tmpl_fd, 0) != 0) {
  85. error("%s: Error setting process contract template "
  86. "critical events: %s", __func__, strerror(errno));
  87. goto fail;
  88. }
  89. if (ct_tmpl_set_informative(tmpl_fd, CT_PR_EV_HWERR) != 0) {
  90. error("%s: Error setting process contract template "
  91. "informative events: %s", __func__, strerror(errno));
  92. goto fail;
  93. }
  94. /* Now make this the active template for this process. */
  95. if (ct_tmpl_activate(tmpl_fd) != 0) {
  96. error("%s: Error activating process contract "
  97. "template: %s", __func__, strerror(errno));
  98. goto fail;
  99. }
  100. return;
  101. fail:
  102. if (tmpl_fd != -1) {
  103. close(tmpl_fd);
  104. tmpl_fd = -1;
  105. }
  106. }
  107. void
  108. solaris_contract_post_fork_child()
  109. {
  110. debug2("%s: clearing process contract template on fd %d",
  111. __func__, tmpl_fd);
  112. /* Clear the active template. */
  113. if (ct_tmpl_clear(tmpl_fd) != 0)
  114. error("%s: Error clearing active process contract "
  115. "template: %s", __func__, strerror(errno));
  116. close(tmpl_fd);
  117. tmpl_fd = -1;
  118. }
  119. void
  120. solaris_contract_post_fork_parent(pid_t pid)
  121. {
  122. ctid_t ctid;
  123. char ctl_path[256];
  124. int r, ctl_fd = -1, stat_fd = -1;
  125. debug2("%s: clearing template (fd %d)", __func__, tmpl_fd);
  126. if (tmpl_fd == -1)
  127. return;
  128. /* First clear the active template. */
  129. if ((r = ct_tmpl_clear(tmpl_fd)) != 0)
  130. error("%s: Error clearing active process contract "
  131. "template: %s", __func__, strerror(errno));
  132. close(tmpl_fd);
  133. tmpl_fd = -1;
  134. /*
  135. * If either the fork didn't succeed (pid < 0), or clearing
  136. * th active contract failed (r != 0), then we have nothing
  137. * more do.
  138. */
  139. if (r != 0 || pid <= 0)
  140. return;
  141. /* Now lookup and abandon the contract we've created. */
  142. ctid = get_active_process_contract_id();
  143. debug2("%s: abandoning contract id %ld", __func__, ctid);
  144. snprintf(ctl_path, sizeof(ctl_path),
  145. CTFS_ROOT "/process/%ld/ctl", ctid);
  146. if ((ctl_fd = open64(ctl_path, O_WRONLY)) < 0) {
  147. error("%s: Error opening process contract "
  148. "ctl file: %s", __func__, strerror(errno));
  149. goto fail;
  150. }
  151. if (ct_ctl_abandon(ctl_fd) < 0) {
  152. error("%s: Error abandoning process contract: %s",
  153. __func__, strerror(errno));
  154. goto fail;
  155. }
  156. close(ctl_fd);
  157. return;
  158. fail:
  159. if (tmpl_fd != -1) {
  160. close(tmpl_fd);
  161. tmpl_fd = -1;
  162. }
  163. if (stat_fd != -1)
  164. close(stat_fd);
  165. if (ctl_fd != -1)
  166. close(ctl_fd);
  167. }
  168. #endif
  169. #ifdef USE_SOLARIS_PROJECTS
  170. #include <sys/task.h>
  171. #include <project.h>
  172. /*
  173. * Get/set solaris default project.
  174. * If we fail, just run along gracefully.
  175. */
  176. void
  177. solaris_set_default_project(struct passwd *pw)
  178. {
  179. struct project *defaultproject;
  180. struct project tempproject;
  181. char buf[1024];
  182. /* get default project, if we fail just return gracefully */
  183. if ((defaultproject = getdefaultproj(pw->pw_name, &tempproject, &buf,
  184. sizeof(buf))) != NULL) {
  185. /* set default project */
  186. if (setproject(defaultproject->pj_name, pw->pw_name,
  187. TASK_NORMAL) != 0)
  188. debug("setproject(%s): %s", defaultproject->pj_name,
  189. strerror(errno));
  190. } else {
  191. /* debug on getdefaultproj() error */
  192. debug("getdefaultproj(%s): %s", pw->pw_name, strerror(errno));
  193. }
  194. }
  195. #endif /* USE_SOLARIS_PROJECTS */
  196. #ifdef USE_SOLARIS_PRIVS
  197. # ifdef HAVE_PRIV_H
  198. # include <priv.h>
  199. # endif
  200. priv_set_t *
  201. solaris_basic_privset(void)
  202. {
  203. priv_set_t *pset;
  204. #ifdef HAVE_PRIV_BASICSET
  205. if ((pset = priv_allocset()) == NULL) {
  206. error("priv_allocset: %s", strerror(errno));
  207. return NULL;
  208. }
  209. priv_basicset(pset);
  210. #else
  211. if ((pset = priv_str_to_set("basic", ",", NULL)) == NULL) {
  212. error("priv_str_to_set: %s", strerror(errno));
  213. return NULL;
  214. }
  215. #endif
  216. return pset;
  217. }
  218. void
  219. solaris_drop_privs_pinfo_net_fork_exec(void)
  220. {
  221. priv_set_t *pset = NULL, *npset = NULL;
  222. /*
  223. * Note: this variant avoids dropping DAC filesystem rights, in case
  224. * the process calling it is running as root and should have the
  225. * ability to read/write/chown any file on the system.
  226. *
  227. * We start with the basic set, then *add* the DAC rights to it while
  228. * taking away other parts of BASIC we don't need. Then we intersect
  229. * this with our existing PERMITTED set. In this way we keep any
  230. * DAC rights we had before, while otherwise reducing ourselves to
  231. * the minimum set of privileges we need to proceed.
  232. *
  233. * This also means we drop any other parts of "root" that we don't
  234. * need (e.g. the ability to kill any process, create new device nodes
  235. * etc etc).
  236. */
  237. if ((pset = priv_allocset()) == NULL)
  238. fatal("priv_allocset: %s", strerror(errno));
  239. if ((npset = solaris_basic_privset()) == NULL)
  240. fatal("solaris_basic_privset: %s", strerror(errno));
  241. if (priv_addset(npset, PRIV_FILE_CHOWN) != 0 ||
  242. priv_addset(npset, PRIV_FILE_DAC_READ) != 0 ||
  243. priv_addset(npset, PRIV_FILE_DAC_SEARCH) != 0 ||
  244. priv_addset(npset, PRIV_FILE_DAC_WRITE) != 0 ||
  245. priv_addset(npset, PRIV_FILE_OWNER) != 0)
  246. fatal("priv_addset: %s", strerror(errno));
  247. if (priv_delset(npset, PRIV_PROC_EXEC) != 0 ||
  248. #ifdef PRIV_NET_ACCESS
  249. priv_delset(npset, PRIV_NET_ACCESS) != 0 ||
  250. #endif
  251. priv_delset(npset, PRIV_PROC_FORK) != 0 ||
  252. priv_delset(npset, PRIV_PROC_INFO) != 0 ||
  253. priv_delset(npset, PRIV_PROC_SESSION) != 0)
  254. fatal("priv_delset: %s", strerror(errno));
  255. if (getppriv(PRIV_PERMITTED, pset) != 0)
  256. fatal("getppriv: %s", strerror(errno));
  257. priv_intersect(pset, npset);
  258. if (setppriv(PRIV_SET, PRIV_PERMITTED, npset) != 0 ||
  259. setppriv(PRIV_SET, PRIV_LIMIT, npset) != 0 ||
  260. setppriv(PRIV_SET, PRIV_INHERITABLE, npset) != 0)
  261. fatal("setppriv: %s", strerror(errno));
  262. priv_freeset(pset);
  263. priv_freeset(npset);
  264. }
  265. void
  266. solaris_drop_privs_root_pinfo_net(void)
  267. {
  268. priv_set_t *pset = NULL;
  269. /* Start with "basic" and drop everything we don't need. */
  270. if ((pset = solaris_basic_privset()) == NULL)
  271. fatal("solaris_basic_privset: %s", strerror(errno));
  272. if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
  273. #ifdef PRIV_NET_ACCESS
  274. priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
  275. #endif
  276. priv_delset(pset, PRIV_PROC_INFO) != 0 ||
  277. priv_delset(pset, PRIV_PROC_SESSION) != 0)
  278. fatal("priv_delset: %s", strerror(errno));
  279. if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
  280. setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
  281. setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
  282. fatal("setppriv: %s", strerror(errno));
  283. priv_freeset(pset);
  284. }
  285. void
  286. solaris_drop_privs_root_pinfo_net_exec(void)
  287. {
  288. priv_set_t *pset = NULL;
  289. /* Start with "basic" and drop everything we don't need. */
  290. if ((pset = solaris_basic_privset()) == NULL)
  291. fatal("solaris_basic_privset: %s", strerror(errno));
  292. if (priv_delset(pset, PRIV_FILE_LINK_ANY) != 0 ||
  293. #ifdef PRIV_NET_ACCESS
  294. priv_delset(pset, PRIV_NET_ACCESS) != 0 ||
  295. #endif
  296. priv_delset(pset, PRIV_PROC_EXEC) != 0 ||
  297. priv_delset(pset, PRIV_PROC_INFO) != 0)
  298. fatal("priv_delset: %s", strerror(errno));
  299. if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) != 0 ||
  300. setppriv(PRIV_SET, PRIV_LIMIT, pset) != 0 ||
  301. setppriv(PRIV_SET, PRIV_INHERITABLE, pset) != 0)
  302. fatal("setppriv: %s", strerror(errno));
  303. priv_freeset(pset);
  304. }
  305. #endif