From: Ian Campbell Subject: Re: [PATCH 2/3] libxl: Hold the atfork lock while closing carefd Date: Mon, 24 Feb 2014 14:47:34 +0000 Message-ID: <1393253254.16570.80.camel@kazak.uk.xensource.com> References: <1393251555-22418-1-git-send-email-ian.jackson@eu.citrix.com> <1393251555-22418-3-git-send-email-ian.jackson@eu.citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1393251555-22418-3-git-send-email-ian.jackson@eu.citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Jackson Cc: George Dunlap , xen-devel@lists.xensource.com, M A Young List-Id: xen-devel@lists.xenproject.org On Mon, 2014-02-24 at 14:19 +0000, Ian Jackson wrote: > This avoids the process being forked while a carefd is recorded in the > list but the actual fd has been closed. If that happened, a > subsequent libxl_postfork_child_noexec would attempt to close the fd > again. If we are lucky that results in a harmless warning; but if we > are unlucky the fd number has been reused and we close an unrelated > fd. > > This race has not been observed anywhere as far as we are aware. > > Signed-off-by: Ian Jackson Acked-by: Ian Campbell > CC: George Dunlap > --- > tools/libxl/libxl_fork.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c > index 8421296..fa15095 100644 > --- a/tools/libxl/libxl_fork.c > +++ b/tools/libxl/libxl_fork.c > @@ -184,9 +184,9 @@ void libxl_postfork_child_noexec(libxl_ctx *ctx) > int libxl__carefd_close(libxl__carefd *cf) > { > if (!cf) return 0; > + atfork_lock(); > int r = cf->fd < 0 ? 0 : close(cf->fd); > int esave = errno; > - atfork_lock(); > LIBXL_LIST_REMOVE(cf, entry); > atfork_unlock(); > free(cf);