chroot.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringView.h>
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. struct Options {
  31. const char* path;
  32. const char* program { "/bin/Shell" };
  33. int flags { -1 };
  34. };
  35. void print_usage(const char* argv0)
  36. {
  37. fprintf(
  38. stderr,
  39. "Usage:\n"
  40. "\t%s <path> [program] [-o options]\n",
  41. argv0
  42. );
  43. }
  44. Options parse_options(int argc, char** argv)
  45. {
  46. Options options;
  47. if (argc < 2) {
  48. print_usage(argv[0]);
  49. exit(1);
  50. }
  51. options.path = argv[1];
  52. int i = 2;
  53. if (i < argc && argv[i][0] != '-')
  54. options.program = argv[i++];
  55. if (i >= argc)
  56. return options;
  57. if (strcmp(argv[i], "-o") != 0) {
  58. print_usage(argv[0]);
  59. exit(1);
  60. }
  61. i++;
  62. if (i >= argc) {
  63. print_usage(argv[0]);
  64. exit(1);
  65. }
  66. options.flags = 0;
  67. StringView arg = argv[i];
  68. Vector<StringView> parts = arg.split_view(',');
  69. for (auto& part : parts) {
  70. if (part == "defaults")
  71. continue;
  72. else if (part == "nodev")
  73. options.flags |= MS_NODEV;
  74. else if (part == "noexec")
  75. options.flags |= MS_NOEXEC;
  76. else if (part == "nosuid")
  77. options.flags |= MS_NOSUID;
  78. else if (part == "bind")
  79. fprintf(stderr, "Ignoring -o bind, as it doesn't make sense for chroot");
  80. else
  81. fprintf(stderr, "Ignoring invalid option: %s\n", String(part).characters());
  82. }
  83. return options;
  84. }
  85. int main(int argc, char** argv)
  86. {
  87. Options options = parse_options(argc, argv);
  88. if (chroot_with_mount_flags(options.path, options.flags) < 0) {
  89. perror("chroot");
  90. return 1;
  91. }
  92. if (chdir("/") < 0) {
  93. perror("chdir(/)");
  94. return 1;
  95. }
  96. execl(options.program, options.program, nullptr);
  97. perror("execl");
  98. return 1;
  99. }