sandboxed-api/sandboxed_api/sandbox2/testcases/namespace.cc
Christian Blichmann 177b969e8c
Sandboxed API OSS release.
PiperOrigin-RevId: 238996664
Change-Id: I9646527e2be68ee0b6b371572b7aafe967102e57

Signed-off-by: Christian Blichmann <cblichmann@google.com>
2019-03-18 19:00:48 +01:00

81 lines
2.2 KiB
C++

// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Checks various things related to namespaces, depending on the first argument:
// ./binary 0 <file1> <file2> ... <fileN>:
// Make sure all provided files exist and are RO, return 0 on OK.
// Returns the index of the first non-existing file + 1 on failure.
// ./binary 1 <file1> <file2> ... <fileN>:
// Make sure all provided files exist and are RW, return 0 on OK.
// Returns the index of the first non-existing file + 1 on failure.
// ./binary 2
// Make sure that we run in a PID namespace (this implies getpid() == 1)
// Returns 0 on OK.
// ./binary 3 <uid> <gid>
// Make sure getuid()/getgid() returns the provided uid/gid (User namespace).
// Returns 0 on OK.
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
int main(int argc, char* argv[]) {
if (argc < 2) {
return 0;
}
int mode = atoi(argv[1]); // NOLINT(runtime/deprecated_fn)
switch (mode) {
case 0: {
// Make sure file exist
for (int i = 2; i < argc; i++) {
if (access(argv[i], R_OK)) {
return i - 1;
}
}
} break;
case 1: {
for (int i = 2; i < argc; i++) {
if (access(argv[i], W_OK)) {
return i - 1;
}
}
} break;
case 2: {
if (getpid() != 2) {
return getpid();
}
} break;
case 3: {
if (argc != 4) {
return 1;
}
if (getuid() != atoi(argv[2]) // NOLINT(runtime/deprecated_fn)
|| getgid() != atoi(argv[3])) { // NOLINT(runtime/deprecated_fn)
return getuid();
}
} break;
default:
return 1;
}
return 0;
}