You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
6.4 KiB

11 years ago
11 years ago
  1. // Copyright 2007-2010 Mir Calculate Ltd. http://www.calculate-linux.org
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. // for password
  18. #include <sys/types.h>
  19. #include <pwd.h>
  20. // for strcpy strlen
  21. #include <string.h>
  22. // for keystore kernel
  23. #include <keyutils.h>
  24. // for statistics file
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. int main( int argc, const char* argv[] )
  28. {
  29. // user id and gid
  30. uid_t uid, gid;
  31. int ret;
  32. int rez1,rez2;
  33. void *buffer;
  34. if (argc==1)
  35. {
  36. // get user id and gid
  37. uid = getuid();
  38. gid = getgid();
  39. // set user id and gid for the process
  40. rez1 = setgid(gid);
  41. rez2 = setuid(uid);
  42. if (rez1==-1||rez2==-1)
  43. {
  44. printf ("exec not SUID root\n");
  45. exit(EXIT_FAILURE);
  46. };
  47. // find user key in keystore
  48. ret = request_key("user", "tmp", NULL, 0);
  49. if (ret < 0)
  50. {
  51. printf ("id_key not found\n");
  52. exit(EXIT_FAILURE);
  53. };
  54. // get user key
  55. int retf;
  56. retf = keyctl_read_alloc(ret, &buffer);
  57. if (retf < 0)
  58. {
  59. printf("error keyctl_read_alloc\n");
  60. exit(EXIT_FAILURE);
  61. };
  62. printf ("%s", (char*) buffer);
  63. buffer = "XXXXXXXX";
  64. key_serial_t dest;
  65. // get id user key
  66. dest = KEY_SPEC_USER_SESSION_KEYRING;
  67. // write key in user space
  68. ret = add_key("user", "tmp", buffer, strlen(buffer), dest);
  69. exit(EXIT_SUCCESS);
  70. };
  71. if (argc!=3)
  72. {
  73. printf("Error: needed two argument\n");
  74. exit(EXIT_FAILURE);
  75. }
  76. // get user id and gid
  77. uid = getuid();
  78. gid = getgid();
  79. char * prog_name[11];
  80. char * prog_path[11];
  81. char * prog_sring[11];
  82. int count_prog =11;
  83. // identifiers and paths to programs
  84. prog_name[0] = "rdesktop";
  85. prog_path[0] = "/usr/bin/rdesktop";
  86. prog_sring[0] = NULL;
  87. prog_name[1] = "rdesktop1";
  88. prog_path[1] = "/usr/bin/rdesktop";
  89. prog_sring[1] = "/usr/bin/kstart --window=.* --desktop=1";
  90. prog_name[2] = "rdesktop2";
  91. prog_path[2] = "/usr/bin/rdesktop";
  92. prog_sring[2] = "/usr/bin/kstart --window=.* --desktop=2";
  93. prog_name[3] = "rdesktop3";
  94. prog_path[3] = "/usr/bin/rdesktop";
  95. prog_sring[3] = "/usr/bin/kstart --window=.* --desktop=3";
  96. prog_name[4] = "rdesktop4";
  97. prog_path[4] = "/usr/bin/rdesktop";
  98. prog_sring[4] = "/usr/bin/kstart --window=.* --desktop=4";
  99. prog_name[5] = "rdesktop5";
  100. prog_path[5] = "/usr/bin/rdesktop";
  101. prog_sring[5] = "/usr/bin/kstart --window=.* --desktop=5";
  102. prog_name[6] = "rdesktop6";
  103. prog_path[6] = "/usr/bin/rdesktop";
  104. prog_sring[6] = "/usr/bin/kstart --window=.* --desktop=6";
  105. prog_name[7] = "rdesktop7";
  106. prog_path[7] = "/usr/bin/rdesktop";
  107. prog_sring[7] = "/usr/bin/kstart --window=.* --desktop=7";
  108. prog_name[8] = "rdesktop8";
  109. prog_path[8] = "/usr/bin/rdesktop";
  110. prog_sring[8] = "/usr/bin/kstart --window=.* --desktop=8";
  111. prog_name[9] = "rdesktop9";
  112. prog_path[9] = "/usr/bin/rdesktop";
  113. prog_sring[9] = "/usr/bin/kstart --window=.* --desktop=9";
  114. prog_name[10] = "xfreerdp";
  115. prog_path[10] = "/usr/bin/xfreerdp";
  116. prog_sring[10] = NULL;
  117. // path to the running program
  118. char * str_prog = NULL;
  119. // if kstart
  120. char * str_prog_ks = NULL;
  121. int i;
  122. for (i=0;i<count_prog;i++)
  123. {
  124. if (strcmp(prog_name[i],argv[1])==0)
  125. {
  126. str_prog = prog_path[i];
  127. str_prog_ks = prog_sring[i];
  128. break;
  129. };
  130. };
  131. if (str_prog == NULL)
  132. {
  133. printf ("False program\n");
  134. exit(EXIT_FAILURE);
  135. };
  136. struct stat bufS;
  137. int res;
  138. int fd;
  139. // file permissions for check
  140. int mode_file = 33261;
  141. fd = open(str_prog, O_RDONLY);
  142. res = fstat(fd,&bufS);
  143. if (res==0)
  144. {
  145. close(fd);
  146. }else
  147. {
  148. printf("No open file %s\n",str_prog);
  149. exit(EXIT_FAILURE);
  150. };
  151. // comparison of permissions and owner of the executable file with the sample
  152. if (bufS.st_mode == mode_file && bufS.st_uid == 0 && bufS.st_gid == 0)
  153. {
  154. struct passwd *pwd = getpwuid (uid);
  155. if (pwd == NULL)
  156. {
  157. exit(EXIT_FAILURE);
  158. };
  159. // get user name
  160. char *login;
  161. login = (char*) malloc (strlen(pwd->pw_name)+1);
  162. strcpy (login,pwd->pw_name);
  163. // set permissions root
  164. rez1 = setgid(0);
  165. rez2 = setuid(0);
  166. if (rez1==-1||rez2==-1)
  167. {
  168. printf ("Exec not SUID root\n");
  169. exit(EXIT_FAILURE);
  170. };
  171. int ret;
  172. // find user key in keystore
  173. ret = request_key("user", login, NULL, 0);
  174. if (ret < 0)
  175. {
  176. printf ("id_key not found\n");
  177. exit(EXIT_FAILURE);
  178. };
  179. // get user key
  180. ret = keyctl_read_alloc(ret, &buffer);
  181. if (ret < 0)
  182. {
  183. printf("error keyctl_read_alloc\n");
  184. exit(EXIT_FAILURE);
  185. }
  186. // set user id and gid for the process
  187. rez1 = setgid(gid);
  188. rez2 = setuid(uid);
  189. if (rez1==-1||rez2==-1)
  190. {
  191. printf ("exec not SUID root\n");
  192. exit(EXIT_FAILURE);
  193. };
  194. key_serial_t dest;
  195. // get id user key
  196. dest = KEY_SPEC_USER_SESSION_KEYRING;
  197. // write key in user space
  198. ret = add_key("user", "tmp", buffer, strlen(buffer), dest);
  199. // allocate memory and create a line running
  200. char *buff;
  201. if (str_prog_ks == NULL)
  202. {
  203. char *com = "keyexec | %s %s";
  204. buff = (char*) malloc (strlen(com)+strlen(str_prog)+strlen(argv[2])+1);
  205. sprintf (buff, com, str_prog, argv[2]);
  206. }
  207. else
  208. {
  209. char *com = "%s keyexec | %s %s";
  210. buff = (char*) malloc (strlen(str_prog_ks)+strlen(com)+strlen(buffer)+strlen(str_prog)+strlen(argv[2])+1);
  211. sprintf (buff, com, str_prog_ks, str_prog, argv[2]);
  212. };
  213. // running program
  214. system(buff);
  215. free(login);
  216. free (buff);
  217. exit(EXIT_SUCCESS);
  218. };
  219. printf ("Executed file %s not valid\n",str_prog);
  220. exit(EXIT_FAILURE);
  221. }