public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 06/10] Replace FWRITE with a function
Date: Thu, 13 Oct 2022 19:51:04 +1100	[thread overview]
Message-ID: <Y0fReL8aThnBRiJY@yekko> (raw)
In-Reply-To: <20221013041709.316ec052@elisabeth>

[-- Attachment #1: Type: text/plain, Size: 5958 bytes --]

On Thu, Oct 13, 2022 at 04:17:09AM +0200, Stefano Brivio wrote:
> On Tue, 11 Oct 2022 16:40:14 +1100
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > In a few places we use the FWRITE() macro to open a file, replace it's
> > contents with a given string and close it again.  There's no real
> > reason this needs to be a macro rather than just a function though.
> 
> Well, there's a bit of a reason: it gives more descriptive messages in
> isolate_user() with the same LoCs.
> 
> On the other hand I would also prefer a function here, probably better
> than the alternative I'm not sure why this series needs this by the
> way.
> 
> > Turn it into a function 'write_file()' and make some ancillary
> > cleanups while we're there:
> >   - Add a return code so the caller can handle giving a useful error message
> >   - Handle the case of short write()s (unlikely, but possible)
> >   - Add O_TRUNC, to make sure we replace the existing contents entirely
> > 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  isolation.c | 17 +++++++++--------
> >  pasta.c     |  4 ++--
> >  util.c      | 33 +++++++++++++++++++++++++++++++++
> >  util.h      | 13 +------------
> >  4 files changed, 45 insertions(+), 22 deletions(-)
> > 
> > diff --git a/isolation.c b/isolation.c
> > index 10cef05..4aa75e6 100644
> > --- a/isolation.c
> > +++ b/isolation.c
> > @@ -129,7 +129,8 @@ void isolate_initial(void)
> >   */
> >  void isolate_user(uid_t uid, gid_t gid, bool use_userns, const char *userns)
> >  {
> > -	char nsmap[BUFSIZ];
> > +	char uidmap[BUFSIZ];
> > +	char gidmap[BUFSIZ];
> >  
> >  	/* First set our UID & GID in the original namespace */
> >  	if (setgroups(0, NULL)) {
> > @@ -184,14 +185,14 @@ void isolate_user(uid_t uid, gid_t gid, bool use_userns, const char *userns)
> >  	}
> >  
> >  	/* Configure user and group mappings */
> > -	snprintf(nsmap, BUFSIZ, "0 %u 1", uid);
> > -	FWRITE("/proc/self/uid_map", nsmap, "Cannot set uid_map in namespace");
> > +	snprintf(uidmap, BUFSIZ, "0 %u 1", uid);
> > +	snprintf(gidmap, BUFSIZ, "0 %u 1", gid);
> >  
> > -	FWRITE("/proc/self/setgroups", "deny",
> > -	       "Cannot write to setgroups in namespace");
> > -
> > -	snprintf(nsmap, BUFSIZ, "0 %u 1", gid);
> > -	FWRITE("/proc/self/gid_map", nsmap, "Cannot set gid_map in namespace");
> > +	if (write_file("/proc/self/uid_map", uidmap) ||
> > +	    write_file("/proc/self/setgroups", "deny") ||
> > +	    write_file("/proc/self/gid_map", gidmap)) {
> > +		warn("Couldn't configure user namespace");
> 
> It's unlikely that we can write to uid_map but not to setgroups. Still,
> having separate messages, as we had them, could help investigating some
> issues.

Right, but there is also a message in write_file() itself.

> > +	}
> >  }
> >  
> >  /**
> > diff --git a/pasta.c b/pasta.c
> > index 4ff322c..0ab2fe4 100644
> > --- a/pasta.c
> > +++ b/pasta.c
> > @@ -167,8 +167,8 @@ static int pasta_setup_ns(void *arg)
> >  	const struct pasta_setup_ns_arg *a
> >  		= (const struct pasta_setup_ns_arg *)arg;
> >  
> > -	FWRITE("/proc/sys/net/ipv4/ping_group_range", "0 0",
> > -	       "Cannot set ping_group_range, ICMP requests might fail");
> > +	if (write_file("/proc/sys/net/ipv4/ping_group_range", "0 0"))
> > +		warn("Cannot set ping_group_range, ICMP requests might fail");
> >  
> >  	execvp(a->exe, a->argv);
> >  
> > diff --git a/util.c b/util.c
> > index 6b86ead..93b93b1 100644
> > --- a/util.c
> > +++ b/util.c
> > @@ -547,3 +547,36 @@ int fls(unsigned long x)
> >  
> >  	return y;
> >  }
> > +
> > +/**
> > + * write_file() - Replace contents of file with a string
> > + * @path:	File to write
> > + * @buf:	String to write
> > + *
> > + * Return: 0 on success, -1 on any error
> > + */
> > +int write_file(const char *path, const char *buf)
> 
> We could also use this for the PID file by optionally taking a file number, but
> I haven't tried how it looks like.

Yeah, maybe later.

> > +{
> > +	int fd = open(path, O_WRONLY | O_TRUNC | O_CLOEXEC);
> > +	size_t len = strlen(buf);
> > +
> > +	if (fd < 0) {
> > +		warn("Could not open %s: %s", path, strerror(errno));
> > +		return -1;
> > +	}
> > +
> > +	while (len) {
> > +		ssize_t rc = write(fd, buf, len);
> > +
> > +		if (rc < 0) {
> 
> I would change this to <= 0. Not that it matters with write(), but
> should we ever change that another function, we run the (absolutely
> not critical) risk of forgetting to adjust this and get stuck in a loop
> here.

Fair enough, done.

> > +			warn("Couldn't write to %s: %s", path, strerror(errno));
> > +			break;
> > +		}
> > +
> > +		buf += rc;
> > +		len -= rc;
> > +	}
> > +
> > +	close(fd);
> > +	return len == 0 ? 0 : -1;
> > +}
> > diff --git a/util.h b/util.h
> > index 0c06e34..f957522 100644
> > --- a/util.h
> > +++ b/util.h
> > @@ -58,18 +58,6 @@ void trace_init(int enable);
> >  #define TMPDIR		"/tmp"
> >  #endif
> >  
> > -#define FWRITE(path, buf, str)						\
> > -	do {								\
> > -		int flags = O_WRONLY | O_CLOEXEC;			\
> > -		int fd = open(path, flags);				\
> > -									\
> > -		if (fd < 0 ||						\
> > -		    write(fd, buf, strlen(buf)) != (int)strlen(buf))	\
> > -			warn(str);					\
> > -		if (fd >= 0)						\
> > -			close(fd);					\
> > -	} while (0)
> > -
> >  #define V4		0
> >  #define V6		1
> >  #define IP_VERSIONS	2
> > @@ -215,5 +203,6 @@ int ns_enter(const struct ctx *c);
> >  void write_pidfile(int fd, pid_t pid);
> >  int __daemon(int pidfile_fd, int devnull_fd);
> >  int fls(unsigned long x);
> > +int write_file(const char *path, const char *buf);
> >  
> >  #endif /* UTIL_H */
> 

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2022-10-13  9:37 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11  5:40 [PATCH 00/10] Fixes and cleanups for capability handling David Gibson
2022-10-11  5:40 ` [PATCH 01/10] test: Move slower tests to end of test run David Gibson
2022-10-11  5:40 ` [PATCH 02/10] pasta: More general way of starting spawned shell as a login shell David Gibson
2022-10-13  2:16   ` Stefano Brivio
2022-10-13  8:22     ` David Gibson
2022-10-13  9:48       ` Stefano Brivio
2022-10-13 23:24         ` David Gibson
2022-10-11  5:40 ` [PATCH 03/10] pasta_start_ns() always ends in parent context David Gibson
2022-10-11  5:40 ` [PATCH 04/10] Remove unhelpful drop_caps() call in pasta_start_ns() David Gibson
2022-10-11  5:40 ` [PATCH 05/10] Clarify various self-isolation steps David Gibson
2022-10-13  2:17   ` Stefano Brivio
2022-10-13  8:31     ` David Gibson
2022-10-13 12:49   ` Stefano Brivio
2022-10-13 23:25     ` David Gibson
2022-10-11  5:40 ` [PATCH 06/10] Replace FWRITE with a function David Gibson
2022-10-13  2:17   ` Stefano Brivio
2022-10-13  8:51     ` David Gibson [this message]
2022-10-11  5:40 ` [PATCH 07/10] isolation: Replace drop_caps() with a version that actually does something David Gibson
2022-10-13  2:18   ` Stefano Brivio
2022-10-13  9:44     ` David Gibson
2022-10-13  4:01   ` Stefano Brivio
2022-10-13 13:08     ` Stefano Brivio
2022-10-13 16:37       ` Stefano Brivio
2022-10-13 23:42         ` David Gibson
2022-10-11  5:40 ` [PATCH 08/10] isolation: Prevent any child processes gaining capabilities David Gibson
2022-10-13  2:17   ` Stefano Brivio
2022-10-13  9:33     ` David Gibson
2022-10-13  9:50       ` Stefano Brivio
2022-10-11  5:40 ` [PATCH 09/10] isolation: Only configure UID/GID mappings in userns when spawning shell David Gibson
2022-10-13  2:18   ` Stefano Brivio
2022-10-13  9:36     ` David Gibson
2022-10-11  5:40 ` [PATCH 10/10] Rename pasta_setup_ns() to pasta_spawn_cmd() David Gibson
2022-10-13  2:44 ` [PATCH 00/10] Fixes and cleanups for capability handling Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y0fReL8aThnBRiJY@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).