From nobody Fri Mar 25 14:50:41 2022 X-Original-To: freebsd-current@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 8570F1A4DD26 for ; Fri, 25 Mar 2022 14:50:51 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KQ4lZ2x52z53VY; Fri, 25 Mar 2022 14:50:50 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mail-qt1-x82b.google.com with SMTP id bp39so6675938qtb.6; Fri, 25 Mar 2022 07:50:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=J/RyFR9n/LBVliyXAxJ9KoviBQxVf7ExEkE4PP0+SlI=; b=anywS2BgIQUS7MQHXuVwjYqGXhUJYpY201Kf3miYkWFWmqzypdH2OA8ER/q+EJ2zEf Ova8+XUQzEKeu3O5/MLoRDUZ7Y9iuoxZs6+7OZ+KRXMBr6Q7kBHECGWR5qP7rCMO85l6 A7xswYr0mUrNIXhmOE3BIc8sQxlBLy83zSSuxA9Rs1OMXcbdVppBDWCr6DCXSrt33wHT wgraoE3NYWsCOPXmE73Nebq52jBsHU7wQgez6W1a3n6PuPo91pFbaaS5dwqUsZTAMe+O 88PSjb704L25PrRecBkpMm/d60/iQ5E9NlFWz/NQlap/Xr4Gl4fi02sZ7liHSdfWpTMb DZFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=J/RyFR9n/LBVliyXAxJ9KoviBQxVf7ExEkE4PP0+SlI=; b=Vi9Xi0onSLS5PAHSkJNxHvpQdgEKFewZElYRmCnd+1M+GBITHBOAHkOq+2g22/W4zt 3agysT1cAdIdV3bHjJbt4ynkEH6iitc3hfUR786U45TybERwOOMTXkkOlWmDUKSAEdBO om9nSAidk/SANspuv55izdiypElCEbsbseQE7AxcmAdWEcHlL1/atmWWlOuutOZD/9Zm WZuDzoOFEoo2qEwJMT1KOlGl6tIE6/rUnG8A3Hgz5uqfEAo+peSwjpr7PLuxITTQU/FY muy0Ml9KR22gwiwabj648Fi6m3+3eAmOmKfuAjxhzz8fwT246gymS7BuZZuGlwpag9uw SrPg== X-Gm-Message-State: AOAM530sB4XS6ovPJFIuJPQulD4vpl+AM7Y4otCD77lTYtVVg/X1hI2V fOzECR0pKQ3qR3iQ7V6xN577ZKMz2u4cug== X-Google-Smtp-Source: ABdhPJzn6vaTjon3Nrog9mWQb5e2gxifkX8w5FEcxIjX4Bbx2nfIyCNlZ0CsfJEpjzeFUcri9Hkc+A== X-Received: by 2002:a05:622a:1315:b0:2e2:2952:11b8 with SMTP id v21-20020a05622a131500b002e2295211b8mr9649747qtk.244.1648219844505; Fri, 25 Mar 2022 07:50:44 -0700 (PDT) Received: from nuc (198-84-189-58.cpe.teksavvy.com. [198.84.189.58]) by smtp.gmail.com with ESMTPSA id p8-20020a05620a22e800b0067e75955f5esm3212083qki.77.2022.03.25.07.50.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 07:50:43 -0700 (PDT) Date: Fri, 25 Mar 2022 10:50:41 -0400 From: Mark Johnston To: Guy Yur Cc: freebsd-current , Konstantin Belousov Subject: Re: Interrupted fputc followed by fprintf in _IOLBF mode causes core dump Message-ID: References: List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 4KQ4lZ2x52z53VY X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20210112 header.b=anywS2Bg; dmarc=none; spf=pass (mx1.freebsd.org: domain of markjdb@gmail.com designates 2607:f8b0:4864:20::82b as permitted sender) smtp.mailfrom=markjdb@gmail.com X-Spamd-Result: default: False [-0.57 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; FREEMAIL_TO(0.00)[gmail.com]; FORGED_SENDER(0.30)[markj@freebsd.org,markjdb@gmail.com]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; FROM_NEQ_ENVFROM(0.00)[markj@freebsd.org,markjdb@gmail.com]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.88)[-0.883]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20210112]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; NEURAL_SPAM_SHORT(1.00)[0.999]; NEURAL_HAM_LONG(-0.98)[-0.983]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[freebsd.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::82b:from]; MLMMJ_DEST(0.00)[freebsd-current]; MID_RHS_NOT_FQDN(0.50)[]; RCVD_TLS_ALL(0.00)[] X-ThisMailContainsUnwantedMimeParts: N On Fri, Mar 25, 2022 at 03:18:40PM +0300, Guy Yur wrote: > Hi, > > dhcpcd on head (Mar 24) and 13.1-BETA2 crashes in fprintf/__sfvwrite. > It doesn't crash if If I revert the __sflush/__sfvwrite commits: > 86a16ada1ea608408cec370171d9f59353e97c77 and > bafaa70b6f9098d83d074968c8e6747ecec1e118. > > Stack trace: > 0 memcpy () at /usr/src/lib/libc/amd64/string/memmove.S:314 > #1 0x00000008221c300a in __sfvwrite (fp=, > uio=0x8207ad338) at /usr/src/lib/libc/stdio/fvwrite.c:182 > #2 0x00000008221cc631 in __sprint (fp=0x26fffe, uio=0x8207ad2d7, > locale=) at /usr/src/lib/libc/stdio/vfprintf.c:166 > #3 io_flush (iop=0x8207ad330, locale=) at > /usr/src/lib/libc/stdio/printfcommon.h:157 > #4 __vfprintf (fp=fp@entry=0x8222892d0, locale=locale@entry=0x822288ab8 > <__xlocale_global_locale>, fmt0=, fmt0@entry=0x204182 "%s", > ap=, ap@entry=0x8207ad4e0) at > /usr/src/lib/libc/stdio/vfprintf.c:1033 > #5 0x00000008221c8aea in vfprintf_l (fp=0x8222892d0, locale=0x822288ab8 > <__xlocale_global_locale>, fmt0=0x204182 "%s", ap=0x8207ad4e0) > at /usr/src/lib/libc/stdio/vfprintf.c:285 > #6 0x0000000000222efa in vlogprintf_r (ctx=0x270820 <_logctx>, > stream=0x8222892d0, fmt=0x204182 "%s", args=0x8207adad0) at logerr.c:186 > ... > > (gdb) frame 5 > #5 0x00000008221c8aea in vfprintf_l (fp=0x8222892d0, locale=0x822288ab8 > <__xlocale_global_locale>, fmt0=0x204182 "%s", ap=0x8207ad4e0) > at /usr/src/lib/libc/stdio/vfprintf.c:285 > 285 ret = __vfprintf(fp, locale, fmt0, ap); > (gdb) p *fp > $1 = {_p = 0x27084f <_logctx+47> "e21:3e7c\n42a/64\n", _r = 0, _w = > -1025, _flags = 2057, _file = 2, _bf = {_base = 0x270820 <_logctx> > "*\"", _size = 1024}, > _lbfsize = -1024, _cookie = 0x8222892d0, _close = 0x8221c7b40 > <__sclose>, _read = 0x8221c7af0 <__sread>, _seek = 0x8221c7b30 > <__sseek>, > _write = 0x8221c7b10 <__swrite>, _ub = {_base = 0x0, _size = 0}, _up > = 0x0, _ur = 0, _ubuf = "\000\000", _nbuf = "", _lb = {_base = 0x0, > _size = 0}, > _blksize = 4096, _offset = 0, _fl_mutex = 0x0, _fl_owner = 0x0, > _fl_count = 0, _orientation = -1, _mbstate = {__mbstate8 = '\000' > , > _mbstateL = 0}, _flags2 = 0} > > (gdb) frame 1 > #1 0x00000008221c300a in __sfvwrite (fp=, > uio=0x8207ad338) at /usr/src/lib/libc/stdio/fvwrite.c:182 > 182 COPY(w); > (gdb) p w > $4 = -1 > > > The dhcpcd flow leading to the crash: > 1. init with setvbuf _IOLBF on stderr > https://github.com/NetworkConfiguration/dhcpcd/blob/master/src/logerr.c#L453 > > 2. fputc with newline called on stderr but is interrupted > https://github.com/NetworkConfiguration/dhcpcd/blob/master/src/logerr.c#L187 > > 3. next event received, vfprintf is called on stderr and crashes > https://github.com/NetworkConfiguration/dhcpcd/blob/master/src/logerr.c#L186 > > > Simple program that eventually crashes: Thanks for the reproducer. The bug is akin to that fixed by bafaa70b6f9098d83d074968c8e6747ecec1e118. Could you please verify that the patch below fixes the original bug? commit 11f817e847b2f06bd543d1bd6cfdff53d69842dc Author: Mark Johnston Date: Fri Mar 25 10:46:24 2022 -0400 libc: Restore fp state upon flush error in fputc diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c index e1aa70243e94..6cd75145a271 100644 --- a/lib/libc/stdio/wbuf.c +++ b/lib/libc/stdio/wbuf.c @@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$"); int __swbuf(int c, FILE *fp) { + unsigned char *old_p; int n; /* @@ -87,8 +88,15 @@ __swbuf(int c, FILE *fp) } fp->_w--; *fp->_p++ = c; - if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) - if (__fflush(fp)) + old_p = fp->_p; + if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) { + if (__fflush(fp)) { + if (fp->_p == old_p) { + fp->_p--; + fp->_w++; + } return (EOF); + } + } return (c); }