Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/ping_tool.h" |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 6 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 9 | #include "debugd/src/error_utils.h" |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 10 | #include "debugd/src/process_with_id.h" |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 11 | #include "debugd/src/variant_utils.h" |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 12 | |
| 13 | namespace debugd { |
| 14 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | const char kSetuidHack[] = |
| 18 | "/usr/libexec/debugd/helpers/minijail-setuid-hack.sh"; |
| 19 | const char kPing[] = "/bin/ping"; |
| 20 | const char kPing6[] = "/bin/ping6"; |
| 21 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 22 | const char kPingToolErrorString[] = "org.chromium.debugd.error.Ping"; |
| 23 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 24 | } // namespace |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 25 | |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 26 | bool PingTool::Start(const base::ScopedFD& outfd, |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 27 | const std::string& destination, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 28 | const brillo::VariantDictionary& options, |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 29 | std::string* out_id, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 30 | brillo::ErrorPtr* error) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 31 | ProcessWithId* p = CreateProcess( |
| 32 | true /* sandboxed */, false /* access_root_mount_ns */, |
| 33 | {"-pvrl", "--profile=minimalistic-mountns", "--uts", "-k", |
| 34 | "tmpfs,/run,tmpfs,MS_NODEV|MS_NOEXEC|MS_NOSUID,mode=755,size=10M", |
Ben Chan | b135136 | 2019-02-20 11:14:20 -0800 | [diff] [blame] | 35 | // A /run/shill bind mount is needed to access /etc/resolv.conf, which |
| 36 | // is a symlink to /run/shill/resolv.conf. |
Mike Frysinger | 56379d7 | 2019-02-19 16:03:03 -0500 | [diff] [blame] | 37 | "-b", "/run/shill"}); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 38 | if (!p) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 39 | DEBUGD_ADD_ERROR(error, kPingToolErrorString, |
| 40 | "Could not create ping process"); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 41 | return false; |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 42 | } |
Mike Frysinger | 9bab10a | 2015-05-18 03:35:32 -0400 | [diff] [blame] | 43 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 44 | p->AddArg(kSetuidHack); |
| 45 | if (brillo::GetVariantValueOrDefault<bool>(options, "v6")) |
Mike Frysinger | 9bab10a | 2015-05-18 03:35:32 -0400 | [diff] [blame] | 46 | p->AddArg(kPing6); |
| 47 | else |
| 48 | p->AddArg(kPing); |
| 49 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 50 | if (options.count("broadcast") == 1) |
Paul Stewart | c0479d8 | 2013-02-04 10:02:53 -0800 | [diff] [blame] | 51 | p->AddArg("-b"); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 52 | if (!AddIntOption(p, options, "count", "-c", error)) |
| 53 | return false; |
| 54 | if (!AddIntOption(p, options, "interval", "-i", error)) |
| 55 | return false; |
| 56 | if (options.count("numeric") == 1) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 57 | p->AddArg("-n"); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 58 | if (!AddIntOption(p, options, "packetsize", "-s", error)) |
| 59 | return false; |
| 60 | if (!AddIntOption(p, options, "waittime", "-W", error)) |
| 61 | return false; |
| 62 | |
Alex Khouderchah | 258d3ca | 2019-07-31 17:40:54 -0700 | [diff] [blame] | 63 | auto interface = options.find("interface"); |
| 64 | if (interface != options.end()) { |
| 65 | p->AddStringOption("-I", interface->second.Get<std::string>()); |
| 66 | } |
| 67 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 68 | p->AddArg(destination); |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 69 | p->BindFd(outfd.get(), STDOUT_FILENO); |
| 70 | p->BindFd(outfd.get(), STDERR_FILENO); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 71 | LOG(INFO) << "ping: running process id: " << p->id(); |
| 72 | p->Start(); |
Eric Caruso | 8fe49c7 | 2017-04-25 10:43:59 -0700 | [diff] [blame] | 73 | *out_id = p->id(); |
| 74 | return true; |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 77 | } // namespace debugd |