From patchwork Tue May 11 17:17:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43378 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9DA16398B41F; Tue, 11 May 2021 17:18:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9DA16398B41F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1620753487; bh=1Qjnu3eYolIcsh+uDzrYGmfh4m+cLj6eUm/wNib41Z4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=rugZYAolJ/llfcXon39MN+A1ZBYSbcTT2e1QL1803PeFDAiN6N8UPjVhlzNWM4x3j M7HpxdXDHT7G8cGDvMP3oqgBlyq7ib/joqrVAO2pgjY+7uwIpfZtkvQKt4zhAg/9ah c2klaqd2g6xTQav4wB4NYWNyQGRnLlczMX1adr08= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from donkey.elm.relay.mailchannels.net (donkey.elm.relay.mailchannels.net [23.83.212.49]) by sourceware.org (Postfix) with ESMTPS id 120733865474 for ; Tue, 11 May 2021 17:18:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 120733865474 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 7939178204A for ; Tue, 11 May 2021 17:18:03 +0000 (UTC) Received: from pdx1-sub0-mail-a29.g.dreamhost.com (100-96-133-96.trex.outbound.svc.cluster.local [100.96.133.96]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id F3FEC78204D for ; Tue, 11 May 2021 17:18:02 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a29.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.133.96 (trex/6.2.1); Tue, 11 May 2021 17:18:03 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Glossy-Plucky: 6ca1314c51e9dc92_1620753483209_3323897575 X-MC-Loop-Signature: 1620753483208:659947959 X-MC-Ingress-Time: 1620753483208 Received: from pdx1-sub0-mail-a29.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a29.g.dreamhost.com (Postfix) with ESMTP id 6C19E7EDB6 for ; Tue, 11 May 2021 10:18:02 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a29.g.dreamhost.com (Postfix) with ESMTPSA id 2B07E7EFB8 for ; Tue, 11 May 2021 10:18:00 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a29 To: libc-alpha@sourceware.org Subject: [PATCH] login: Limit strncpy to one less than buffer size Date: Tue, 11 May 2021 22:47:53 +0530 Message-Id: <20210511171753.360387-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3494.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Avoid posibility of ut_line being an unterminated string. --- login/login.c | 2 +- login/logout.c | 2 +- login/logwtmp.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/login/login.c b/login/login.c index d280c13f1f..0822d36753 100644 --- a/login/login.c +++ b/login/login.c @@ -111,7 +111,7 @@ login (const struct utmp *ut) ttyp = basename (tty); /* Position to record for this tty. */ - strncpy (copy.ut_line, ttyp, UT_LINESIZE); + strncpy (copy.ut_line, ttyp, sizeof (copy.ut_line) - 1); /* Tell that we want to use the UTMP file. */ if (utmpname (_PATH_UTMP) == 0) diff --git a/login/logout.c b/login/logout.c index 3def97fc83..8978bd32bf 100644 --- a/login/logout.c +++ b/login/logout.c @@ -38,7 +38,7 @@ logout (const char *line) /* Fill in search information. */ tmp.ut_type = USER_PROCESS; - strncpy (tmp.ut_line, line, sizeof tmp.ut_line); + strncpy (tmp.ut_line, line, sizeof (tmp.ut_line) - 1); /* Read the record. */ if (getutline_r (&tmp, &utbuf, &ut) >= 0) diff --git a/login/logwtmp.c b/login/logwtmp.c index 1a7c46e9c0..bb8f13852d 100644 --- a/login/logwtmp.c +++ b/login/logwtmp.c @@ -33,9 +33,9 @@ logwtmp (const char *line, const char *name, const char *host) memset (&ut, 0, sizeof (ut)); ut.ut_pid = getpid (); ut.ut_type = name[0] ? USER_PROCESS : DEAD_PROCESS; - strncpy (ut.ut_line, line, sizeof ut.ut_line); - strncpy (ut.ut_name, name, sizeof ut.ut_name); - strncpy (ut.ut_host, host, sizeof ut.ut_host); + strncpy (ut.ut_line, line, sizeof (ut.ut_line) - 1); + strncpy (ut.ut_name, name, sizeof (ut.ut_name) - 1); + strncpy (ut.ut_host, host, sizeof (ut.ut_host) - 1); struct __timespec64 ts; __clock_gettime64 (CLOCK_REALTIME, &ts);