runcon.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * runcon [ context |
  3. * ( [ -c ] [ -r role ] [-t type] [ -u user ] [ -l levelrange ] )
  4. * command [arg1 [arg2 ...] ]
  5. *
  6. * attempt to run the specified command with the specified context.
  7. *
  8. * -r role : use the current context with the specified role
  9. * -t type : use the current context with the specified type
  10. * -u user : use the current context with the specified user
  11. * -l level : use the current context with the specified level range
  12. * -c : compute process transition context before modifying
  13. *
  14. * Contexts are interpreted as follows:
  15. *
  16. * Number of MLS
  17. * components system?
  18. *
  19. * 1 - type
  20. * 2 - role:type
  21. * 3 Y role:type:range
  22. * 3 N user:role:type
  23. * 4 Y user:role:type:range
  24. * 4 N error
  25. *
  26. * Port to busybox: KaiGai Kohei <kaigai@kaigai.gr.jp>
  27. * - based on coreutils-5.97 (in Fedora Core 6)
  28. */
  29. #include <getopt.h>
  30. #include <selinux/context.h>
  31. #include <selinux/flask.h>
  32. #include "libbb.h"
  33. static context_t runcon_compute_new_context(char *user, char *role, char *type, char *range,
  34. char *command, int compute_trans)
  35. {
  36. context_t con;
  37. security_context_t cur_context;
  38. if (getcon(&cur_context))
  39. bb_error_msg_and_die("cannot get current context");
  40. if (compute_trans) {
  41. security_context_t file_context, new_context;
  42. if (getfilecon(command, &file_context) < 0)
  43. bb_error_msg_and_die("cannot retrieve attributes of '%s'",
  44. command);
  45. if (security_compute_create(cur_context, file_context,
  46. SECCLASS_PROCESS, &new_context))
  47. bb_error_msg_and_die("unable to compute a new context");
  48. cur_context = new_context;
  49. }
  50. con = context_new(cur_context);
  51. if (!con)
  52. bb_error_msg_and_die("'%s' is not a valid context", cur_context);
  53. if (user && context_user_set(con, user))
  54. bb_error_msg_and_die("failed to set new user '%s'", user);
  55. if (type && context_type_set(con, type))
  56. bb_error_msg_and_die("failed to set new type '%s'", type);
  57. if (range && context_range_set(con, range))
  58. bb_error_msg_and_die("failed to set new range '%s'", range);
  59. if (role && context_role_set(con, role))
  60. bb_error_msg_and_die("failed to set new role '%s'", role);
  61. return con;
  62. }
  63. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  64. static const struct option runcon_options[] = {
  65. { "user", 1, NULL, 'u' },
  66. { "role", 1, NULL, 'r' },
  67. { "type", 1, NULL, 't' },
  68. { "range", 1, NULL, 'l' },
  69. { "compute", 0, NULL, 'c' },
  70. { "help", 0, NULL, 'h' },
  71. { NULL, 0, NULL, 0 },
  72. };
  73. #endif
  74. #define OPTS_ROLE (1<<0) /* r */
  75. #define OPTS_TYPE (1<<1) /* t */
  76. #define OPTS_USER (1<<2) /* u */
  77. #define OPTS_RANGE (1<<3) /* l */
  78. #define OPTS_COMPUTE (1<<4) /* c */
  79. #define OPTS_HELP (1<<5) /* h */
  80. #define OPTS_CONTEXT_COMPONENT (OPTS_ROLE | OPTS_TYPE | OPTS_USER | OPTS_RANGE)
  81. int runcon_main(int argc, char **argv);
  82. int runcon_main(int argc, char **argv)
  83. {
  84. char *role = NULL;
  85. char *range = NULL;
  86. char *user = NULL;
  87. char *type = NULL;
  88. char *context = NULL;
  89. unsigned opts;
  90. context_t con;
  91. selinux_or_die();
  92. #if ENABLE_FEATURE_RUNCON_LONG_OPTIONS
  93. applet_long_options = runcon_options;
  94. #endif
  95. opt_complementary = "-1";
  96. opts = getopt32(argc, argv, "r:t:u:l:ch", &role, &type, &user, &range);
  97. argv += optind;
  98. if (!(opts & OPTS_CONTEXT_COMPONENT)) {
  99. context = *argv++;
  100. if (!argv[0])
  101. bb_error_msg_and_die("no command given");
  102. }
  103. if (context) {
  104. con = context_new(context);
  105. if (!con)
  106. bb_error_msg_and_die("'%s' is not a valid context", context);
  107. } else {
  108. con = runcon_compute_new_context(user, role, type, range,
  109. argv[0], opts & OPTS_COMPUTE);
  110. }
  111. if (security_check_context(context_str(con)))
  112. bb_error_msg_and_die("'%s' is not a valid context",
  113. context_str(con));
  114. if (setexeccon(context_str(con)))
  115. bb_error_msg_and_die("cannot set up security context '%s'",
  116. context_str(con));
  117. execvp(argv[0], argv);
  118. bb_perror_msg_and_die("cannot execute '%s'", argv[0]);
  119. }