diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 5ccc408cd..3c97fb8ef 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -29,7 +29,7 @@ jobs: build-mode: manual - name: Install build dependencies - run: sudo apt-get update -qq && sudo apt-get install -qq libarchive-dev libtalloc-dev pkg-config swig uthash-dev python3-dev lzop + run: sudo apt-get update -qq && sudo apt-get install -qq libarchive-dev libprotobuf-c-dev libtalloc-dev pkg-config swig uthash-dev python3-dev lzop - name: Build run: make -C src clean loader.elf loader-m32.elf build.h proot care diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 9442c0013..82e562667 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -53,7 +53,7 @@ jobs: - name: Install build dependencies run: | sudo apt-get update -qq - sudo apt-get install -qq check libtalloc-dev pkg-config + sudo apt-get install -qq check libprotobuf-c-dev libtalloc-dev pkg-config - name: Run the libcheck suite run: make -C test/unit check @@ -77,7 +77,7 @@ jobs: - name: Install build dependencies run: | sudo apt-get update -qq - sudo apt-get install -qq bats clang-tools curl gdb lcov libarchive-dev libtalloc-dev pkg-config sloccount strace swig uthash-dev python3-dev lzop + sudo apt-get install -qq bats clang-tools curl gdb lcov libarchive-dev libprotobuf-c-dev libtalloc-dev pkg-config sloccount strace swig uthash-dev python3-dev lzop - name: Gather analytics run: sloccount --details . @@ -130,7 +130,7 @@ jobs: - name: Install cross toolchain and target libraries run: | sudo apt-get install -qq -y crossbuild-essential-arm64 pkg-config uthash-dev qemu-user \ - libarchive-dev:arm64 libtalloc-dev:arm64 + libarchive-dev:arm64 libprotobuf-c-dev:arm64 libtalloc-dev:arm64 - name: Cross-build proot and care for aarch64 run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b3cceae62..dad22702d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,7 +37,7 @@ jobs: - name: Install build dependencies run: | sudo apt-get update -qq - sudo apt-get install -qq libarchive-dev libtalloc-dev pkg-config uthash-dev lzop g++ + sudo apt-get install -qq libarchive-dev libprotobuf-c-dev libtalloc-dev pkg-config uthash-dev lzop g++ - name: Build static proot and care binaries run: | diff --git a/doc/proot/stylesheets/rpm-spec.xsl b/doc/proot/stylesheets/rpm-spec.xsl index 497de088c..df58ce638 100644 --- a/doc/proot/stylesheets/rpm-spec.xsl +++ b/doc/proot/stylesheets/rpm-spec.xsl @@ -15,7 +15,7 @@ Buildroot : %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) Prefix : /usr Name : proot -BuildRequires: libtalloc-devel +BuildRequires: libtalloc-devel, protobuf-c-devel, libattr-devel %if 0%{?suse_version} >= 1210 || 0%{?fedora_version} >= 15 BuildRequires: glibc-static diff --git a/src/GNUmakefile b/src/GNUmakefile index 24662e806..bef70387c 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -1,6 +1,8 @@ # If you want to build outside of the source tree, use the -f option: # make -f ${SOMEWHERE}/proot/src/GNUmakefile +PERSISTENT_CHOWN := 1 + # the VPATH variable must point to the actual makefile directory VPATH := $(dir $(lastword $(MAKEFILE_LIST))) SRC = $(dir $(firstword $(MAKEFILE_LIST))) @@ -41,6 +43,12 @@ HAS_PYTHON_CONFIG := $(shell ${PYTHON}-config --ldflags ${PYTHON_EMBED} 2>/dev/n endif CPPFLAGS += -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I. -I$(VPATH) -I$(VPATH)/../lib/uthash/include +ifdef PERSISTENT_CHOWN + CPPFLAGS += -DPERSISTENT_CHOWN + CPPFLAGS += $(shell ${PKG_CONFIG} --cflags libprotobuf-c) + HAS_ATTR_XATTR := $(shell if [ -f /usr/include/attr/xattr.h ]; then echo -DHAS_ATTR_XATTR; fi) + CPPFLAGS += ${HAS_ATTR_XATTR} +endif CFLAGS += -g -Wall -Wextra -O2 # pkg-config silently returns an empty string for --cflags/--libs when it @@ -59,6 +67,9 @@ endif CFLAGS += $(shell ${PKG_CONFIG} --cflags talloc) LDFLAGS += -Wl,-z,noexecstack LDFLAGS += $(shell ${PKG_CONFIG} --libs talloc) +ifdef PERSISTENT_CHOWN + LDFLAGS += $(shell ${PKG_CONFIG} --libs libprotobuf-c) +endif ifneq ($(findstring -static,$(LDFLAGS)),) CARE_LDFLAGS = -Wl,--start-group $(shell ${PKG_CONFIG} --libs --static libarchive) \ @@ -109,6 +120,10 @@ OBJECTS += \ extension/portmap/map.o \ loader/loader-wrapped.o +ifdef PERSISTENT_CHOWN + OBJECTS += rootlesscontainers/rootlesscontainers.pb-c.o +endif + define define_from_arch.h $2$1 := $(shell $(CC) $1 -E -dM -DNO_LIBC_HEADER $(SRC)/arch.h | grep -w $2 | cut -f 3 -d ' ') endef diff --git a/src/cli/cli.c b/src/cli/cli.c index f98a6ffce..6e641a830 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -24,7 +24,9 @@ #include /* bool, true, false, */ #include /* ARG_MAX, PATH_MAX, */ #include /* str*(3), */ +#if !defined(__GLIBC__) #include /* basename(3), */ +#endif #include /* talloc*, */ #include /* exit(3), EXIT_*, strtol(3), {g,s}etenv(3), */ #include /* assert(3), */ diff --git a/src/extension/fake_id0/fake_id0.c b/src/extension/fake_id0/fake_id0.c index f51ab46a5..9d680e250 100644 --- a/src/extension/fake_id0/fake_id0.c +++ b/src/extension/fake_id0/fake_id0.c @@ -44,6 +44,89 @@ #include "path/binding.h" #include "arch.h" +#if defined(PERSISTENT_CHOWN) +#include +#if defined(HAS_ATTR_XATTR) +#include +#else +#include +#endif +#include "path/path.h" +#include "rootlesscontainers/rootlesscontainers.pb-c.h" +#define XATTR_USER_ROOTLESSCONTAINERS "user.rootlesscontainers" + +#ifndef ENOATTR +# define ENOATTR ENODATA +#endif + +// return 0 on success. +int rootlesscontainers_get_xattr (int fd, const char *path, uid_t *uid, gid_t *gid) { + Rootlesscontainers__Resource *msg; + void *buf; + assert(fd >= 0 || path != NULL); + ssize_t bufsz = (path != NULL) ? + lgetxattr(path, XATTR_USER_ROOTLESSCONTAINERS, NULL, 0) : + fgetxattr(fd, XATTR_USER_ROOTLESSCONTAINERS, NULL, 0); + // errno can be ENOATTR, EBADF... Typically not critical. + if (bufsz <= 0) + return bufsz; + buf = malloc(bufsz); + if (buf == NULL) + return -1; + bufsz = (path != NULL) ? + lgetxattr(path, XATTR_USER_ROOTLESSCONTAINERS, buf, bufsz) : + fgetxattr(fd, XATTR_USER_ROOTLESSCONTAINERS, buf, bufsz); + if (bufsz <= 0) + return bufsz; + msg = rootlesscontainers__resource__unpack(NULL, bufsz, buf); + if (msg == NULL) { + free(buf); + return -1; + } + if (!msg->has_uid) + msg->uid = (uid_t)-1; + if (!msg->has_gid) + msg->gid = (gid_t)-1; + *uid = (uid_t)msg->uid; + *gid = (gid_t)msg->gid; + free(buf); + rootlesscontainers__resource__free_unpacked(msg, NULL); + return 0; +} + +int rootlesscontainers_set_xattr (int fd, const char *path, uid_t uid, gid_t gid) { + int rc; + Rootlesscontainers__Resource msg = ROOTLESSCONTAINERS__RESOURCE__INIT; + void *buf; + size_t bufsz; + assert(fd >= 0 || path != NULL); + msg.has_uid = true; + msg.uid = (uint32_t)uid; + msg.has_gid = true; + msg.gid = (uint32_t)gid; + bufsz = rootlesscontainers__resource__get_packed_size(&msg); + buf = malloc(bufsz); + if (buf == NULL) + return -1; + rootlesscontainers__resource__pack(&msg, buf); + rc = (path != NULL) ? + lsetxattr(path, XATTR_USER_ROOTLESSCONTAINERS, buf, bufsz, 0): + fsetxattr(fd, XATTR_USER_ROOTLESSCONTAINERS, buf, bufsz, 0); + free(buf); + return rc; +} + +// ENOATTR is ignored +int rootlesscontainers_remove_xattr (int fd, const char *path) { + assert(fd >= 0 || path != NULL); + int rc = (path != NULL) ? + lremovexattr(path, XATTR_USER_ROOTLESSCONTAINERS): + fremovexattr(fd, XATTR_USER_ROOTLESSCONTAINERS); + return ( rc < 0 && errno == ENOATTR) ? 0 : rc; +} + +#endif /* if defined(PERSISTENT_CHOWN) */ + typedef struct { uid_t ruid; uid_t euid; @@ -238,7 +321,7 @@ static void override_permissions(const Tracee *tracee, const char *path, /** * Adjust current @tracee's syscall parameters according to @config. - * This function always returns 0. + * This function returns 0 unless there is an error. */ static int handle_sysenter_end(Tracee *tracee, const Config *config) { @@ -277,6 +360,12 @@ static int handle_sysenter_end(Tracee *tracee, const Config *config) Reg gid_sysarg; uid_t uid; gid_t gid; +#if defined(PERSISTENT_CHOWN) + int status; + int fd = -1; // shut up gcc uninitialization warning + char path[PATH_MAX]; + bool with_path = (sysnum == PR_chown || sysnum == PR_chown32 || sysnum == PR_lchown || sysnum == PR_lchown32 || sysnum == PR_fchownat); +#endif if (sysnum == PR_fchownat) { uid_sysarg = SYSARG_3; @@ -289,12 +378,48 @@ static int handle_sysenter_end(Tracee *tracee, const Config *config) uid = peek_reg(tracee, ORIGINAL, uid_sysarg); gid = peek_reg(tracee, ORIGINAL, gid_sysarg); +#if defined(PERSISTENT_CHOWN) + if (with_path) { + word_t input; + char guestpath[PATH_MAX]; + int dirfd = AT_FDCWD; + if (sysnum == PR_fchownat) { + dirfd = peek_reg(tracee, ORIGINAL, SYSARG_1); + input = peek_reg(tracee, ORIGINAL, SYSARG_2); + status = read_path(tracee, guestpath, input); + } else { + input = peek_reg(tracee, ORIGINAL, SYSARG_1); + status = read_path(tracee, guestpath, input); + } + if (status < 0) + return status; + status = translate_path(tracee, path, dirfd, guestpath, false); + if (status < 0) + return status; + } else { + fd = peek_reg(tracee, ORIGINAL, SYSARG_1); + } + + // TODO: we can also remove xattr when [ug]id == -1 && the recoded one == config->r[ug]id + if (uid != config->ruid || gid != config->rgid) + status = rootlesscontainers_set_xattr(fd, with_path ? path : NULL, uid, gid); + else + status = rootlesscontainers_remove_xattr(fd, with_path ? path : NULL); + + if (status < 0) + return status; + + /* this should always succeed */ + poke_reg(tracee, uid_sysarg, getuid()); + poke_reg(tracee, gid_sysarg, getgid()); +#else /* Swap actual and emulated ids to get a chance of * success. */ if (uid == config->ruid) poke_reg(tracee, uid_sysarg, getuid()); if (gid == config->rgid) poke_reg(tracee, gid_sysarg, getgid()); +#endif return 0; } @@ -633,6 +758,14 @@ static int handle_sysexit_end(Tracee *tracee, Config *config) gid_t gid; off_t uid_offset; off_t gid_offset; +#if defined(PERSISTENT_CHOWN) + int status; + char path[PATH_MAX]; + bool with_path = !(sysnum == PR_fstat64 || sysnum == PR_fstat); + int fd = -1; + uid_t uid_xattr; + gid_t gid_xattr; +#endif /* Override only if it succeed. */ result = peek_reg(tracee, CURRENT, SYSARG_RESULT); @@ -676,6 +809,36 @@ static int handle_sysexit_end(Tracee *tracee, Config *config) if (gid == getgid()) poke_uint32(tracee, address + gid_offset, config->sgid); +#if defined(PERSISTENT_CHOWN) + if (with_path) { + word_t input; + char guestpath[PATH_MAX]; + int dirfd = AT_FDCWD; + if (sysnum == PR_fstatat64 || sysnum == PR_newfstatat || sysnum == PR_statx) { + dirfd = peek_reg(tracee, ORIGINAL, SYSARG_1); + input = peek_reg(tracee, ORIGINAL, SYSARG_2); + status = read_path(tracee, guestpath, input); + } else { + input = peek_reg(tracee, ORIGINAL, SYSARG_1); + status = read_path(tracee, guestpath, input); + } + if (status < 0) + return status; + status = translate_path(tracee, path, dirfd, guestpath, false); + if (status < 0) + return status; + } else { + fd = peek_reg(tracee, ORIGINAL, SYSARG_1); + } + status = rootlesscontainers_get_xattr(fd, with_path ? path: NULL, &uid_xattr, &gid_xattr); + // error is not critical, typically. e.g. ENOATTR + if (status >= 0) { + if (uid_xattr != (uid_t)-1) + poke_uint32(tracee, address + uid_offset, uid_xattr); + if (gid_xattr != (gid_t)-1) + poke_uint32(tracee, address + gid_offset, gid_xattr); + } +#endif return 0; } diff --git a/src/rootlesscontainers/COPYING.APACHE2 b/src/rootlesscontainers/COPYING.APACHE2 new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/src/rootlesscontainers/COPYING.APACHE2 @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/src/rootlesscontainers/README.md b/src/rootlesscontainers/README.md new file mode 100644 index 000000000..acc6fb854 --- /dev/null +++ b/src/rootlesscontainers/README.md @@ -0,0 +1,13 @@ +# rootlesscontainers.proto + +This directory contains `rootlesscontainers.proto`, which is used for preserving emulated file owner information as `user.rootlesscontainers` xattr values. + +## Source + +https://raw.githubusercontent.com/rootless-containers/rootlesscontaine.rs/f7b0f5486bb0e0be75771ce50c54471296f040eb/docs/proto/rootlesscontainers.proto + +## Compile + +```console +$ protoc-c --c_out=. rootlesscontainers.proto +``` diff --git a/src/rootlesscontainers/rootlesscontainers.pb-c.c b/src/rootlesscontainers/rootlesscontainers.pb-c.c new file mode 100644 index 000000000..96753b0e2 --- /dev/null +++ b/src/rootlesscontainers/rootlesscontainers.pb-c.c @@ -0,0 +1,103 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: rootlesscontainers.proto */ + +/* Do not generate deprecated warnings for self */ +#ifndef PROTOBUF_C__NO_DEPRECATED +#define PROTOBUF_C__NO_DEPRECATED +#endif + +#include "rootlesscontainers.pb-c.h" +void rootlesscontainers__resource__init + (Rootlesscontainers__Resource *message) +{ + static Rootlesscontainers__Resource init_value = ROOTLESSCONTAINERS__RESOURCE__INIT; + *message = init_value; +} +size_t rootlesscontainers__resource__get_packed_size + (const Rootlesscontainers__Resource *message) +{ + assert(message->base.descriptor == &rootlesscontainers__resource__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t rootlesscontainers__resource__pack + (const Rootlesscontainers__Resource *message, + uint8_t *out) +{ + assert(message->base.descriptor == &rootlesscontainers__resource__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t rootlesscontainers__resource__pack_to_buffer + (const Rootlesscontainers__Resource *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &rootlesscontainers__resource__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +Rootlesscontainers__Resource * + rootlesscontainers__resource__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (Rootlesscontainers__Resource *) + protobuf_c_message_unpack (&rootlesscontainers__resource__descriptor, + allocator, len, data); +} +void rootlesscontainers__resource__free_unpacked + (Rootlesscontainers__Resource *message, + ProtobufCAllocator *allocator) +{ + assert(message->base.descriptor == &rootlesscontainers__resource__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +static const ProtobufCFieldDescriptor rootlesscontainers__resource__field_descriptors[2] = +{ + { + "uid", + 1, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_UINT32, + offsetof(Rootlesscontainers__Resource, has_uid), + offsetof(Rootlesscontainers__Resource, uid), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "gid", + 2, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_UINT32, + offsetof(Rootlesscontainers__Resource, has_gid), + offsetof(Rootlesscontainers__Resource, gid), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned rootlesscontainers__resource__field_indices_by_name[] = { + 1, /* field[1] = gid */ + 0, /* field[0] = uid */ +}; +static const ProtobufCIntRange rootlesscontainers__resource__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 2 } +}; +const ProtobufCMessageDescriptor rootlesscontainers__resource__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "rootlesscontainers.Resource", + "Resource", + "Rootlesscontainers__Resource", + "rootlesscontainers", + sizeof(Rootlesscontainers__Resource), + 2, + rootlesscontainers__resource__field_descriptors, + rootlesscontainers__resource__field_indices_by_name, + 1, rootlesscontainers__resource__number_ranges, + (ProtobufCMessageInit) rootlesscontainers__resource__init, + NULL,NULL,NULL /* reserved[123] */ +}; diff --git a/src/rootlesscontainers/rootlesscontainers.pb-c.h b/src/rootlesscontainers/rootlesscontainers.pb-c.h new file mode 100644 index 000000000..a999faf36 --- /dev/null +++ b/src/rootlesscontainers/rootlesscontainers.pb-c.h @@ -0,0 +1,86 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: rootlesscontainers.proto */ + +#ifndef PROTOBUF_C_rootlesscontainers_2eproto__INCLUDED +#define PROTOBUF_C_rootlesscontainers_2eproto__INCLUDED + +#include + +PROTOBUF_C__BEGIN_DECLS + +#if PROTOBUF_C_VERSION_NUMBER < 1000000 +# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers. +#elif 1002001 < PROTOBUF_C_MIN_COMPILER_VERSION +# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c. +#endif + + +typedef struct _Rootlesscontainers__Resource Rootlesscontainers__Resource; + + +/* --- enums --- */ + + +/* --- messages --- */ + +/* + * Resource defines the schema for "user.rootlesscontainers" xattr values. + * The resource can be used as a persistent storage for emulated `chown(2)` syscall. + * Syscall emulators SHOULD try to hide this xattr from the emulated environment. + */ +struct _Rootlesscontainers__Resource +{ + ProtobufCMessage base; + /* + * Zero-value MUST be parsed as a literally zero-value, not "unset". + * To keep both uid and gid unchaged, the entire xattr value SHOULD be removed. + * To keep either one of uid or gid unchaged, 0xFFFFFFFF (in other words, + * `(uint32_t) -1`, see also chown(2)) value SHOULD be set. + * (Because some protobuf bindings cannot distinguish "unset" from zero-value.) + */ + protobuf_c_boolean has_uid; + uint32_t uid; + protobuf_c_boolean has_gid; + uint32_t gid; +}; +#define ROOTLESSCONTAINERS__RESOURCE__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&rootlesscontainers__resource__descriptor) \ + , 0,0, 0,0 } + + +/* Rootlesscontainers__Resource methods */ +void rootlesscontainers__resource__init + (Rootlesscontainers__Resource *message); +size_t rootlesscontainers__resource__get_packed_size + (const Rootlesscontainers__Resource *message); +size_t rootlesscontainers__resource__pack + (const Rootlesscontainers__Resource *message, + uint8_t *out); +size_t rootlesscontainers__resource__pack_to_buffer + (const Rootlesscontainers__Resource *message, + ProtobufCBuffer *buffer); +Rootlesscontainers__Resource * + rootlesscontainers__resource__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void rootlesscontainers__resource__free_unpacked + (Rootlesscontainers__Resource *message, + ProtobufCAllocator *allocator); +/* --- per-message closures --- */ + +typedef void (*Rootlesscontainers__Resource_Closure) + (const Rootlesscontainers__Resource *message, + void *closure_data); + +/* --- services --- */ + + +/* --- descriptors --- */ + +extern const ProtobufCMessageDescriptor rootlesscontainers__resource__descriptor; + +PROTOBUF_C__END_DECLS + + +#endif /* PROTOBUF_C_rootlesscontainers_2eproto__INCLUDED */ diff --git a/src/rootlesscontainers/rootlesscontainers.proto b/src/rootlesscontainers/rootlesscontainers.proto new file mode 100644 index 000000000..5bcfdfd96 --- /dev/null +++ b/src/rootlesscontainers/rootlesscontainers.proto @@ -0,0 +1,33 @@ +// rootlesscontainers.proto +// Copyright (C) 2018 Rootless Containers authors +// +// 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. + +syntax = "proto3"; + +// The rootlesscontainers package is maintained at https://rootlesscontaine.rs/ . +// If you want to extend the resource definition, please open a PR. +package rootlesscontainers; + +// Resource defines the schema for "user.rootlesscontainers" xattr values. +// The resource can be used as a persistent storage for emulated `chown(2)` syscall. +// Syscall emulators SHOULD try to hide this xattr from the emulated environment. +message Resource { + // Zero-value MUST be parsed as a literally zero-value, not "unset". + // To keep both uid and gid unchaged, the entire xattr value SHOULD be removed. + // To keep either one of uid or gid unchaged, 0xFFFFFFFF (in other words, + // `(uint32_t) -1`, see also chown(2)) value SHOULD be set. + // (Because some protobuf bindings cannot distinguish "unset" from zero-value.) + uint32 uid = 1; + uint32 gid = 2; +} diff --git a/src/tracee/event.c b/src/tracee/event.c index 3b274bfab..1a0093e1d 100644 --- a/src/tracee/event.c +++ b/src/tracee/event.c @@ -428,12 +428,16 @@ static int handle_tracee_event_kernel_4_8(Tracee *tracee, signal = 0; if (WIFEXITED(tracee_status)) { - last_exit_status = WEXITSTATUS(tracee_status); + int exit_status = WEXITSTATUS(tracee_status); VERBOSE(tracee, 1, "vpid %" PRIu64 ": exited with status %d", - tracee->vpid, last_exit_status); + tracee->vpid, exit_status); terminate_tracee(tracee); + // Avoid overwriting a failure exit code with a success + if ((exit_status != 0) || (last_exit_status < 0)) + last_exit_status = exit_status; } else if (WIFSIGNALED(tracee_status)) { + last_exit_status = 128 + WTERMSIG(tracee_status); check_architecture(tracee); VERBOSE(tracee, 1, "vpid %" PRIu64 ": terminated with signal %d", @@ -659,12 +663,16 @@ int handle_tracee_event(Tracee *tracee, int tracee_status) signal = 0; if (WIFEXITED(tracee_status)) { - last_exit_status = WEXITSTATUS(tracee_status); + int exit_status = WEXITSTATUS(tracee_status); VERBOSE(tracee, 1, "vpid %" PRIu64 ": exited with status %d", - tracee->vpid, last_exit_status); + tracee->vpid, exit_status); terminate_tracee(tracee); + // Avoid overwriting a failure exit code with a success + if ((exit_status != 0) || (last_exit_status < 0)) + last_exit_status = exit_status; } else if (WIFSIGNALED(tracee_status)) { + last_exit_status = 128 + WTERMSIG(tracee_status); check_architecture(tracee); VERBOSE(tracee, 1, "vpid %" PRIu64 ": terminated with signal %d", diff --git a/test/docker/alpine/x86_64/Dockerfile b/test/docker/alpine/x86_64/Dockerfile index cec425e30..1a4c595e7 100644 --- a/test/docker/alpine/x86_64/Dockerfile +++ b/test/docker/alpine/x86_64/Dockerfile @@ -16,6 +16,7 @@ RUN apk add --no-cache \ make \ mcookie \ musl-dev \ + protobuf-c-dev \ py3-docutils \ python3-dev \ strace \ diff --git a/test/docker/centos/x86_64/Dockerfile b/test/docker/centos/x86_64/Dockerfile index 8e270b850..7815efa05 100644 --- a/test/docker/centos/x86_64/Dockerfile +++ b/test/docker/centos/x86_64/Dockerfile @@ -16,6 +16,7 @@ RUN dnf install -y \ lzop \ make \ pkg-config \ + protobuf-c-devel \ python3-devel \ strace \ swig \ diff --git a/test/docker/debian/x86_64/Dockerfile b/test/docker/debian/x86_64/Dockerfile index 671a9772a..249a46b8f 100644 --- a/test/docker/debian/x86_64/Dockerfile +++ b/test/docker/debian/x86_64/Dockerfile @@ -8,6 +8,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ docutils-common \ git \ libarchive-dev \ + libprotobuf-c-dev \ libtalloc-dev \ make \ pkg-config \