[lxc-devel] [lxd/master] forkfile: only open O_RDWR if necessary

brauner on Github lxc-bot at linuxcontainers.org
Fri May 11 10:24:11 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 373 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180511/91ed735c/attachment.bin>
-------------- next part --------------
From 43777333d369f19fe34c4ab884336c33bd398706 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian at brauner.io>
Date: Fri, 11 May 2018 12:22:52 +0200
Subject: [PATCH] forkfile: only open O_RDWR if necessary

Closes #4552.

Signed-off-by: Christian Brauner <christian at brauner.io>
---
 lxd/main_forkfile.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lxd/main_forkfile.go b/lxd/main_forkfile.go
index 6c68c896d..a27f12736 100644
--- a/lxd/main_forkfile.go
+++ b/lxd/main_forkfile.go
@@ -66,7 +66,10 @@ int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is
 	ssize_t link_length;
 
 	if (!is_dir_manip && !is_symlink_manip) {
-		host_fd = open(host, O_RDWR);
+		if (is_put)
+			host_fd = open(host, O_RDWR);
+		else
+			host_fd = open(host, O_RDONLY);
 		if (host_fd < 0) {
 			error("error: open");
 			return -1;
@@ -147,9 +150,10 @@ int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is
 	if (fstatat(AT_FDCWD, container, &st, AT_SYMLINK_NOFOLLOW) < 0)
 		exists = 0;
 
-	container_open_flags = O_RDWR;
 	if (is_put)
-		container_open_flags |= O_CREAT;
+		container_open_flags = O_RDWR | O_CREAT;
+	else
+		container_open_flags = O_RDONLY;
 
 	if (is_put && !is_dir_manip && exists && S_ISDIR(st.st_mode)) {
 		error("error: Path already exists as a directory");


More information about the lxc-devel mailing list