Repository: opencontainers/runc
File: libcontainer/nsenter/cloned_binary.c (C)

Code before:

/* This comes directly from <linux/fcntl.h>. */
#ifndef F_LINUX_SPECIFIC_BASE
#  define F_LINUX_SPECIFIC_BASE 1024
#endif
#ifndef F_ADD_SEALS
#  define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#  define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
#endif
#ifndef F_SEAL_SEAL
#  define F_SEAL_SEAL          0x0001	/* prevent further seals from being set */
#  define F_SEAL_SHRINK        0x0002	/* prevent file from shrinking */
#  define F_SEAL_GROW          0x0004	/* prevent file from growing */
#  define F_SEAL_WRITE         0x0008	/* prevent writes */
#endif
#ifndef F_SEAL_FUTURE_WRITE
#  define F_SEAL_FUTURE_WRITE  0x0010	/* prevent future writes while mapped */
#endif
#ifndef F_SEAL_EXEC
#  define F_SEAL_EXEC          0x0020	/* prevent chmod modifying exec bits */
#endif

#define CLONED_BINARY_ENV "_LIBCONTAINER_CLONED_BINARY"
#define RUNC_MEMFD_COMMENT "runc_cloned:/proc/self/exe"
/*
 * Some of these seals are new in newer kernel versions, but we require a
 * minimum of all of the original write-related seals set as well as
 * F_SEAL_SEAL. However, we opportunistically set newer seals like F_SEAL_EXEC.
 */
#define RUNC_MEMFD_MIN_SEALS \
	(F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)

static void *must_realloc(void *ptr, size_t size)
{
	void *old = ptr;
	do {
		ptr = realloc(old, size);
	} while (!ptr);
	return ptr;
}

/*
 * Verify whether we are currently in a self-cloned program (namely, is
 * /proc/self/exe a memfd). F_GET_SEALS will only succeed for memfds (or rather
 * for shmem files), and we want to be sure it's actually sealed.
 */
static int is_self_cloned(void)
{
	int fd, seals = 0, is_cloned = false;
	struct stat statbuf = { };
	struct statfs fsbuf = { };

Review comment (suggestion):
Maybe
```suggestion
 * Some of these seals are present in newer kernel versions, but we require a
```


Code after:

/* This comes directly from <linux/fcntl.h>. */
#ifndef F_LINUX_SPECIFIC_BASE
#  define F_LINUX_SPECIFIC_BASE 1024
#endif
#ifndef F_ADD_SEALS
#  define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#  define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
#endif
#ifndef F_SEAL_SEAL
#  define F_SEAL_SEAL          0x0001	/* prevent further seals from being set */
#  define F_SEAL_SHRINK        0x0002	/* prevent file from shrinking */
#  define F_SEAL_GROW          0x0004	/* prevent file from growing */
#  define F_SEAL_WRITE         0x0008	/* prevent writes */
#endif
#ifndef F_SEAL_FUTURE_WRITE
#  define F_SEAL_FUTURE_WRITE  0x0010	/* prevent future writes while mapped */
#endif
#ifndef F_SEAL_EXEC
#  define F_SEAL_EXEC          0x0020	/* prevent chmod modifying exec bits */
#endif

#define CLONED_BINARY_ENV "_LIBCONTAINER_CLONED_BINARY"
#define RUNC_MEMFD_COMMENT "runc_cloned:/proc/self/exe"
/*
 * There are newer memfd seals (such as F_SEAL_FUTURE_WRITE and F_SEAL_EXEC),
 * which we use opportunistically. However, this set is the original set of
 * memfd seals, and we require them all to be set to trust our /proc/self/exe
 * if it is a memfd.
 */
#define RUNC_MEMFD_MIN_SEALS \
	(F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE)

static void *must_realloc(void *ptr, size_t size)
{
	void *old = ptr;
	do {
		ptr = realloc(old, size);
	} while (!ptr);
	return ptr;
}

/*
 * Verify whether we are currently in a self-cloned program (namely, is
 * /proc/self/exe a memfd). F_GET_SEALS will only succeed for memfds (or rather
 * for shmem files), and we want to be sure it's actually sealed.
 */
static int is_self_cloned(void)
{
	int fd, seals = 0, is_cloned = false;
	struct stat statbuf = { };