From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id AB20E385E83F for ; Mon, 21 Mar 2022 15:08:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AB20E385E83F Received: by mail-oi1-x235.google.com with SMTP id q129so14506075oif.4 for ; Mon, 21 Mar 2022 08:08:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yTnoFcwIwpEJYGw8nifClSC3HaM9fX/4XeIkvQjN5tw=; b=HeiDkt0Faz3tXUOTInoB8x8ky7BGtDF6NJj32vtwq8MQpl7Km8Sp2Zkfy5WrGZ4NoQ H5fYos64DXriXrKlftHdUrOoo+bNrLkxM0fD2E9jny/hTTGyqzlXnOa/KKWtaVxF3iwT TbZ6Zmd8tglCyPxFTq8f81qNgM15Z5yPz8x4wt4Jsk3HmlUoOmemORPfJzcX2TQk9IR0 IM85C77rxfA6DN/vEyNMQENzxq7Aio53KTBruPVB1jMy6dY/B/eML2QNfj16K4ERYKS4 2OyfGydOxHnD9EpePjUc1bg/f50D1ueNE/ozClduszEAAw1/j4tdAjqnn94Y99mh3Tu0 Aq8Q== X-Gm-Message-State: AOAM531CSkIn2Pb195nTvZsG1EtvGPq2J44hz8OGajYVSwt+IvzGwb+o V58mArGXGq3IOiqQNclIm9Xfu78cNyy+iQ== X-Google-Smtp-Source: ABdhPJwSfjJLsvwP+tAE2SnLEUdB7rAqvXNJe1GgyR2PpW+26AjVS/n3/hGxfXmfcd65K05VHqjLtw== X-Received: by 2002:aca:2311:0:b0:2ec:cb84:c5bb with SMTP id e17-20020aca2311000000b002eccb84c5bbmr10347316oie.246.1647875335720; Mon, 21 Mar 2022 08:08:55 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:f04a:67c7:cbf3:571d]) by smtp.gmail.com with ESMTPSA id o64-20020acad743000000b002ef3b249b9esm3859458oig.58.2022.03.21.08.08.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 08:08:55 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v4 7/7] misc: Use gmtime instead of localtime Date: Mon, 21 Mar 2022 12:08:38 -0300 Message-Id: <20220321150838.898597-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220321150838.898597-1-adhemerval.zanella@linaro.org> References: <20220321150838.898597-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Mar 2022 15:08:58 -0000 We deviate from RFC3164 which states timestamp should be in localtime because although our __localtime_r does not set tzname, the relay still might still use localtime (which does) and if the server timezone changes it might result in wrong timestamp from client. It still does not help if a process switches its TZ setting from a timezone that has leap seconds, to one that doesn't (or vice versa), but this would incur in other problems. It also handles the highly unlikely case where gmtime might return NULL, in this case only the PRI is set to hopefully instruct the relay to get eh TIMESTAMP (as defined by the RFC). Finally it also uses internally the 64 bit time_t interfaces (to avoid y2038 issues on 32 bit legacy architectures). Checked on x86_64-linux-gnu and i686-linux-gnu. --- misc/syslog.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index b184b15eea..62a69cf41a 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -156,11 +156,26 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* "%b %e %H:%M:%S " */ char timestamp[sizeof "MMM DD hh:mm:ss "]; - time_t now = time_now (); + __time64_t now = time64_now (); + + /* Deviate from RFC3164 which states timestamp should be in localtime + because although __localtime_r does not set tzname, the relay might + still use localtime (which does) and if the server timezone changes + it might result in wrong timestamp from client. It still does not help + if a process switches its TZ setting from a timezone that has leap + seconds, to one that doesn't (or vice versa), but this would incur in + other problems as well. */ struct tm now_tm; - __localtime_r (&now, &now_tm); - __strftime_l (timestamp, sizeof timestamp, "%b %e %T ", &now_tm, - _nl_C_locobj_ptr); + struct tm *now_tmp = __gmtime64_r (&now, &now_tm); + bool has_ts = now_tmp != NULL; + + /* In the unlikely case of gmtime_r failure (tm_year out of int range) + skip the hostname so the message is handled as valid PRI but without + TIMESTAMP or invalid TIMESTAMP (which should force the relay to add the + timestamp itself). */ + if (has_ts) + __strftime_l (timestamp, sizeof timestamp, "%h %e %T ", now_tmp, + _nl_C_locobj_ptr); #define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ "<%d>%s %n%s%s%.0d%s: ", \ @@ -168,8 +183,16 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, LogTag == NULL ? __progname : LogTag, \ "[" + (pid == 0), pid, "]" + (pid == 0) - int l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); +#define SYSLOG_HEADER_WITHOUT_TS(__pri, __msgoff) \ + "<%d>: %n", __pri, __msgoff + + int l; + if (has_ts) + l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + else + l = __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); if (0 <= l && l < sizeof bufs) { va_list apc; @@ -197,8 +220,12 @@ __vsyslog_internal(int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; - __snprintf (buf, sizeof buf, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + if (has_ts) + __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + else + __snprintf (bufs, sizeof bufs, + SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); } else { -- 2.32.0