From owner-freebsd-bugs@FreeBSD.ORG Sat Dec 18 01:20:07 2010 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 599961065670 for ; Sat, 18 Dec 2010 01:20:07 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 311FA8FC1F for ; Sat, 18 Dec 2010 01:20:07 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id oBI1K61M086609 for ; Sat, 18 Dec 2010 01:20:06 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id oBI1K6lA086608; Sat, 18 Dec 2010 01:20:06 GMT (envelope-from gnats) Resent-Date: Sat, 18 Dec 2010 01:20:06 GMT Resent-Message-Id: <201012180120.oBI1K6lA086608@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Pedro Giffuni Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8EDE3106567A for ; Sat, 18 Dec 2010 01:19:55 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (unknown [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id 633B28FC1A for ; Sat, 18 Dec 2010 01:19:55 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.4/8.14.4) with ESMTP id oBI1Jts8018829 for ; Sat, 18 Dec 2010 01:19:55 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.4/8.14.4/Submit) id oBI1Jtr8018828; Sat, 18 Dec 2010 01:19:55 GMT (envelope-from nobody) Message-Id: <201012180119.oBI1Jtr8018828@red.freebsd.org> Date: Sat, 18 Dec 2010 01:19:55 GMT From: Pedro Giffuni To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/153261: sed: fix for a race condition (from illumos) X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 18 Dec 2010 01:20:07 -0000 >Number: 153261 >Category: bin >Synopsis: sed: fix for a race condition (from illumos) >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Sat Dec 18 01:20:06 UTC 2010 >Closed-Date: >Last-Modified: >Originator: Pedro Giffuni >Release: 8.1-RELEASE >Organization: >Environment: FreeBSD mogwai.giffuni.net 8.1-RELEASE FreeBSD 8.1-RELEASE #0: Tue Nov 9 10:31:43 UTC 2010 pedro@mogwai.giffuni.net:/usr/src/sys/i386/compile/GENERIC i386 >Description: Garrett D'Amore's blog: http://blogs.nexenta.org/ "I just integrated a new sed(1) port for illumos. This is derived from FreeBSD, but it includes a fix for a race condition, and support for translated messages. (FreeBSD friends, please feel free to include these changes back — I’ve not changed the original BSD license.)" The complete sources are available here: http://mexico.purplecow.org/gdamore/webrev/sed/ I only looked where link was replaced by rename and was careful to apply this patch here: http://www.illumos.org/issues/524 It should also be noted that they made a small change to libc/regex (PR bin/153257) to obtain a Solaris compatible behavior. >How-To-Repeat: >Fix: Patch attached with submission follows: diff -ru sed.orig/main.c sed/main.c --- sed.orig/main.c 2010-12-17 17:42:46.000000000 +0000 +++ sed/main.c 2010-12-17 19:58:41.000000000 +0000 @@ -338,8 +338,10 @@ if (infile != NULL) { fclose(infile); if (*oldfname != '\0') { - if (rename(fname, oldfname) != 0) { - warn("rename()"); + /* if there was a backup file, remove it */ + unlink(oldfname); + if (link(fname, oldfname) != 0) { + warn("link()"); unlink(tmpfname); exit(1); } @@ -347,9 +349,18 @@ } if (*tmpfname != '\0') { if (outfile != NULL && outfile != stdout) - fclose(outfile); + if (fclose(outfile) != 0) { + warn("fclose()"); + unlink(tmpfname); + exit(1); + } outfile = NULL; - rename(tmpfname, fname); + if (rename(tmpfname, fname) != 0) { + /* this should not happen really! */ + warn("rename()"); + unlink(tmpfname); + exit(1); + } *tmpfname = '\0'; } outfname = NULL; >Release-Note: >Audit-Trail: >Unformatted: