Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
varqox committed Oct 13, 2024
2 parents a2d13e8 + c04458f commit c0ee6b6
Show file tree
Hide file tree
Showing 24 changed files with 845 additions and 156 deletions.
7 changes: 5 additions & 2 deletions subprojects/sim/src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sim/internal_files/old_internal_file.hh>
#include <sim/mysql/mysql.hh>
#include <sim/sql/sql.hh>
#include <simlib/am_i_root.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/config_file.hh>
#include <simlib/directory.hh>
Expand Down Expand Up @@ -279,10 +280,8 @@ void save(ArgSeq::Iter begin, ArgSeq::Iter end) {
"--password-command=echo sim",
"backup",
".db.config",
"bin",
"dump.sql",
"internal_files",
"lib",
"logs",
"manage",
"sim.conf",
Expand Down Expand Up @@ -416,6 +415,10 @@ void restore(ArgSeq::Iter begin, ArgSeq::Iter end) {
} // namespace

int main(int argc, char** argv) {
if (am_i_root() != AmIRoot::NO) {
die_with_error("This program should not be run as root.");
}

try {
auto args = to_arg_seq(argc, argv);
if (args.begin() == args.end()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <simlib/inplace_buff.hh>
#include <simlib/logger.hh>
#include <simlib/sim/conver.hh>
#include <simlib/sim/judge_worker.hh>
#include <simlib/sim/problem_package.hh>
#include <utility>

Expand Down Expand Up @@ -175,6 +176,21 @@ std::vector<FileRemover> submit_solutions(
for (const auto& solution : simfile.solutions) {
logger("Submitting: ", solution);
auto solution_file_id = sim::internal_files::new_internal_file_id(mysql, current_datetime);
decltype(Submission::language) language = [&] {
// NOLINTNEXTLINE(bugprone-switch-missing-default-case)
switch (sim::filename_to_lang(solution)) {
case sim::SolutionLanguage::UNKNOWN: break;
case sim::SolutionLanguage::C11: return Submission::Language::C11;
case sim::SolutionLanguage::CPP11: return Submission::Language::CPP11;
case sim::SolutionLanguage::CPP14: return Submission::Language::CPP14;
case sim::SolutionLanguage::CPP17: return Submission::Language::CPP17;
case sim::SolutionLanguage::CPP20: return Submission::Language::CPP20;
case sim::SolutionLanguage::PASCAL: return Submission::Language::PASCAL;
case sim::SolutionLanguage::PYTHON: return Submission::Language::PYTHON;
case sim::SolutionLanguage::RUST: return Submission::Language::RUST;
}
THROW("unexpected language");
}();
mysql.execute(
InsertInto("submissions (created_at, file_id, user_id, problem_id, contest_problem_id, "
"contest_round_id, contest_id, type, language, initial_status, full_status, "
Expand All @@ -185,7 +201,7 @@ std::vector<FileRemover> submit_solutions(
solution_file_id,
problem_id,
Submission::Type::PROBLEM_SOLUTION,
sim::filename_to_lang(solution),
language,
Submission::Status::PENDING,
Submission::Status::PENDING
)
Expand Down
3 changes: 2 additions & 1 deletion subprojects/sim/src/job_server/job_handlers/judge_logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <optional>
#include <simlib/concat_tostr.hh>
#include <simlib/escape_bytes_to_utf8_str.hh>
#include <simlib/from_unsafe.hh>
#include <simlib/sim/judge/test_report.hh>
#include <simlib/sim/judge_worker.hh>
Expand Down Expand Up @@ -80,7 +81,7 @@ public:
case sim::JudgeReport::Test::SKIPPED: back_insert(line, "\033[1;36mSKIPPED\033[m"); break;
}
if (!test_report.comment.empty()) {
back_insert(line, " (", test_report.comment, ')');
back_insert(line, " (", escape_bytes_to_utf8_str("", test_report.comment, ""), ')');
}

back_insert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sim/submissions/submission.hh>
#include <sim/submissions/update_final.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/escape_bytes_to_utf8_str.hh>
#include <simlib/macros/stack_unwinding.hh>
#include <simlib/sim/judge_worker.hh>
#include <simlib/string_transform.hh>
Expand Down Expand Up @@ -298,7 +299,7 @@ void judge_or_rejudge_submission(
"<li><span class=\"test-id\">",
html_escape(test.name),
"</span>",
html_escape(test.comment),
html_escape(escape_bytes_to_utf8_str("", test.comment, "")),
"</li>"
);
}
Expand Down
6 changes: 6 additions & 0 deletions subprojects/sim/src/job_server/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <sim/mysql/mysql.hh>
#include <sim/mysql/repeat_if_deadlocked.hh>
#include <sim/sql/sql.hh>
#include <simlib/am_i_root.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/concurrent/bounded_queue.hh>
#include <simlib/concurrent/mutexed_value.hh>
Expand Down Expand Up @@ -440,6 +441,11 @@ int main() {
return 1;
}

if (am_i_root() != AmIRoot::NO) {
errlog("This program should not be run as root.");
return 1;
}

// Terminate older instances
kill_processes_by_exec({executable_path(getpid())}, std::chrono::seconds(4), true);

Expand Down
17 changes: 17 additions & 0 deletions subprojects/sim/src/manage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <csignal>
#include <cstdlib>
#include <fcntl.h>
#include <simlib/am_i_root.hh>
#include <simlib/argv_parser.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/errmsg.hh>
Expand All @@ -11,6 +12,7 @@
#include <simlib/process.hh>
#include <simlib/string_view.hh>
#include <simlib/syscalls.hh>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread>
Expand Down Expand Up @@ -95,6 +97,16 @@ struct SimPaths {

static void stop() {
SimPaths paths;
// Lock the sim directory during killing to avoid situation where two instances wanting to start
// sim kill each other before starting sim.
auto sim_dir = path_dirpath(paths.manage).to_string();
auto sim_dir_fd = FileDescriptor{sim_dir.c_str(), O_RDONLY};
if (flock(sim_dir_fd, LOCK_EX)) {
errlog("flock()", errmsg());
_exit(EXIT_FAILURE);
}
// Lock is freed when the sim_dir_fd is closed

// First kill manage so that it won't restart servers
kill_processes_by_exec({paths.manage}, std::chrono::seconds(1), true);
// Kill servers
Expand Down Expand Up @@ -229,6 +241,11 @@ int main(int argc, char** argv) {
stdlog.use(stdout);
stdlog.label(false);

if (am_i_root() != AmIRoot::NO) {
errlog("This program should not be run as root.");
return 1;
}

auto cmd_options = parse_cmd_options(argc, argv);
if (argc < 2) {
command::help(argv[0]);
Expand Down
6 changes: 6 additions & 0 deletions subprojects/sim/src/sim_merger/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <exception>
#include <sim/db/schema.hh>
#include <sim/mysql/mysql.hh>
#include <simlib/am_i_root.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/file_path.hh>
#include <simlib/from_unsafe.hh>
Expand All @@ -30,6 +31,11 @@ static void help(const char* program_name) {
}

int main(int argc, char** argv) {
if (am_i_root() != AmIRoot::NO) {
errlog("This program should not be run as root.");
return 1;
}

if (argc != 2) {
help(argv[0]);
return 1;
Expand Down
8 changes: 7 additions & 1 deletion subprojects/sim/src/sim_upgrader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sim/submissions/submission.hh>
#include <sim/submissions/update_final.hh>
#include <sim/users/user.hh>
#include <simlib/am_i_root.hh>
#include <simlib/concat.hh>
#include <simlib/concat_tostr.hh>
#include <simlib/config_file.hh>
Expand Down Expand Up @@ -238,10 +239,15 @@ static int true_main(int argc, char** argv) {
}

int main(int argc, char** argv) {
if (am_i_root() != AmIRoot::NO) {
errlog("This program should not be run as root.");
return 1;
}

try {
return true_main(argc, argv);
} catch (const std::exception& e) {
ERRLOG_CATCH(e);
throw;
return 1;
}
}
6 changes: 6 additions & 0 deletions subprojects/sim/src/web_server/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdint>
#include <netinet/in.h>
#include <pthread.h>
#include <simlib/am_i_root.hh>
#include <simlib/config_file.hh>
#include <simlib/file_descriptor.hh>
#include <simlib/process.hh>
Expand Down Expand Up @@ -86,6 +87,11 @@ int main() {
return 1;
}

if (am_i_root() != AmIRoot::NO) {
errlog("This program should not be run as root.");
return 1;
}

// Terminate older instances
kill_processes_by_exec({executable_path(getpid())}, std::chrono::seconds(4), true);

Expand Down
10 changes: 10 additions & 0 deletions subprojects/simlib/include/simlib/am_i_root.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

enum class AmIRoot {
YES,
NO,
MAYBE,
};

// Checks if effective UID or effective GID is equal to root UID / GID. Works in linux namespaces.
AmIRoot am_i_root();
8 changes: 8 additions & 0 deletions subprojects/simlib/include/simlib/escape_bytes_to_utf8_str.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <string>
#include <string_view>

std::string escape_bytes_to_utf8_str(
const std::string_view& prefix, const std::string_view& bytes, const std::string_view& suffix
);
5 changes: 3 additions & 2 deletions subprojects/simlib/include/simlib/sim/judge_worker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdint>
#include <memory>
#include <simlib/concat.hh>
#include <simlib/escape_bytes_to_utf8_str.hh>
#include <simlib/file_manip.hh>
#include <simlib/file_path.hh>
#include <simlib/logger.hh>
Expand Down Expand Up @@ -115,7 +116,7 @@ public:

// Comment
if (!test.comment.empty()) {
back_insert(res, ' ', test.comment, '\n');
back_insert(res, ' ', escape_bytes_to_utf8_str("", test.comment, ""), '\n');
} else {
res += '\n';
}
Expand Down Expand Up @@ -299,7 +300,7 @@ public:
case JudgeReport::Test::SKIPPED: tmplog("\033[1;36mSKIPPED\033[m"); break;
}
if (!test_report.comment.empty()) {
tmplog(" (", test_report.comment, ')');
tmplog(" (", escape_bytes_to_utf8_str("", test_report.comment, ""), ')');
}

// Rest
Expand Down
3 changes: 3 additions & 0 deletions subprojects/simlib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ simlib = library('simlib',
include_directories : simlib_incdir,
sources : [
'src/aho_corasick.cc',
'src/am_i_root.cc',
'src/config_file.cc',
'src/escape_bytes_to_utf8_str.cc',
'src/event_queue.cc',
'src/file_contents.cc',
'src/file_manip.cc',
Expand Down Expand Up @@ -310,6 +312,7 @@ tests = {
'test/enum_val.cc': {},
'test/err_defer.cc': {},
'test/errmsg.cc': {},
'test/escape_bytes_to_utf8_str.cc': {},
'test/event_queue.cc': {},
'test/fd_pread_buff.cc': {},
'test/file_contents.cc': {},
Expand Down
30 changes: 30 additions & 0 deletions subprojects/simlib/src/am_i_root.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <simlib/am_i_root.hh>
#include <simlib/errmsg.hh>
#include <simlib/macros/throw.hh>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>

AmIRoot am_i_root() {
struct stat st;
if (stat("/dev/null", &st)) {
THROW("stat()", errmsg());
}
bool dev_null_is_dev_null = S_ISCHR(st.st_mode) && st.st_rdev == makedev(1, 3);
auto root_real_uid = st.st_uid;
auto root_real_gid = st.st_gid;

auto euid = geteuid();
auto egid = getegid();

if (euid == 0 || egid == 0) {
return AmIRoot::YES;
}
if (!dev_null_is_dev_null) {
return AmIRoot::MAYBE;
}
if (euid == root_real_uid || egid == root_real_gid) {
return AmIRoot::YES;
}
return AmIRoot::NO;
}
Loading

0 comments on commit c0ee6b6

Please sign in to comment.