From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 92939 invoked by alias); 17 Jan 2020 14:05:00 -0000 Mailing-List: contact libc-stable-help@sourceware.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: List-Archive: Sender: libc-stable-owner@sourceware.org Received: (qmail 92816 invoked by uid 89); 17 Jan 2020 14:04:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.3 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-18.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy= X-Spam-Status: No, score=-18.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on sourceware.org X-Spam-Level: X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 17 Jan 2020 14:04:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579269887; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=eiqHipHvrNXcXV69k7vQ60bsOnnkIENPBUpMv7uzMXg=; b=ZOusWjc6/ubQrF4Nz6UZsOzYJEc1gEPmwA5vA/WY3cn8GJxMaAo1YDKFHNMTO2RBz6hZ1e /O/PsKrCbxeB59NJQLn0/uDhL2mRQuRLllyemn1xJy3z6FooRKDWfZtljxsB8T5jmB8SRs zityGg6YyoEd1ysFeV1aoEYq9aM7MJI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-363-MtyOnbF7PL-jo4N48RY10Q-1; Fri, 17 Jan 2020 09:04:41 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A106800D41 for ; Fri, 17 Jan 2020 14:04:40 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-117-165.ams2.redhat.com [10.36.117.165]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 589B380F40 for ; Fri, 17 Jan 2020 14:04:40 +0000 (UTC) Received: by oldenburg2.str.redhat.com (Postfix, from userid 1000) id CECA28299EE3; Fri, 17 Jan 2020 15:04:38 +0100 (CET) Date: Wed, 01 Jan 2020 00:00:00 -0000 To: libc-stable@sourceware.org Subject: [2.30 COMMITTED] login: Introduce matches_last_entry to utmp processing User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20200117140438.CECA28299EE3@oldenburg2.str.redhat.com> From: Florian Weimer X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: MtyOnbF7PL-jo4N48RY10Q-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2020-01/txt/msg00010.txt.bz2 This simplifies internal_getut_nolock and fixes a regression, introduced in commit be6b16d975683e6cca57852cd4cfe715b2a9d8b1 ("login: Acquire write lock early in pututline [BZ #24882]") in pututxline because __utmp_equal can only compare process-related utmp entries. Fixes: be6b16d975683e6cca57852cd4cfe715b2a9d8b1 Change-Id: Ib8a85002f7f87ee41590846d16d7e52bdb82f5a5 (cherry picked from commit 76a7c103eb9060f9e3ba01d073ae4621a17d8b46) diff --git a/login/utmp_file.c b/login/utmp_file.c index d1612ff2b5..b28fb762d7 100644 --- a/login/utmp_file.c +++ b/login/utmp_file.c @@ -43,6 +43,25 @@ static off64_t file_offset; /* Cache for the last read entry. */ static struct utmp last_entry; +/* Returns true if *ENTRY matches last_entry, based on + data->ut_type. */ +static bool +matches_last_entry (const struct utmp *data) +{ + if (file_offset <= 0) + /* Nothing has been read. last_entry is stale and cannot match. */ + return false; + + if (data->ut_type == RUN_LVL + || data->ut_type == BOOT_TIME + || data->ut_type == OLD_TIME + || data->ut_type == NEW_TIME) + /* For some entry types, only a type match is required. */ + return data->ut_type == last_entry.ut_type; + else + /* For the process-related entries, a full match is needed. */ + return __utmp_equal (&last_entry, data); +} /* Locking timeout. */ #ifndef TIMEOUT @@ -133,9 +152,6 @@ __libc_setutent (void) __lseek64 (file_fd, 0, SEEK_SET); file_offset = 0; - /* Make sure the entry won't match. */ - last_entry.ut_type = -1; - return 1; } @@ -191,48 +207,20 @@ __libc_getutent_r (struct utmp *buffer, struct utmp **result) static int internal_getut_nolock (const struct utmp *id) { - if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME - || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME) + while (1) { - /* Search for next entry with type RUN_LVL, BOOT_TIME, - OLD_TIME, or NEW_TIME. */ - - while (1) + /* Read the next entry. */ + if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) + != sizeof (struct utmp)) { - /* Read the next entry. */ - if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) - != sizeof (struct utmp)) - { - __set_errno (ESRCH); - file_offset = -1l; - return -1; - } - file_offset += sizeof (struct utmp); - - if (id->ut_type == last_entry.ut_type) - break; + __set_errno (ESRCH); + file_offset = -1l; + return -1; } - } - else - { - /* Search for the next entry with the specified ID and with type - INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */ + file_offset += sizeof (struct utmp); - while (1) - { - /* Read the next entry. */ - if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp)) - != sizeof (struct utmp)) - { - __set_errno (ESRCH); - file_offset = -1l; - return -1; - } - file_offset += sizeof (struct utmp); - - if (__utmp_equal (&last_entry, id)) - break; - } + if (matches_last_entry (id)) + break; } return 0; @@ -365,13 +353,7 @@ __libc_pututline (const struct utmp *data) /* Find the correct place to insert the data. */ bool found = false; - if (file_offset > 0 - && ((last_entry.ut_type == data->ut_type - && (last_entry.ut_type == RUN_LVL - || last_entry.ut_type == BOOT_TIME - || last_entry.ut_type == OLD_TIME - || last_entry.ut_type == NEW_TIME)) - || __utmp_equal (&last_entry, data))) + if (matches_last_entry (data)) { if (__lseek64 (file_fd, file_offset, SEEK_SET) < 0) { @@ -389,7 +371,7 @@ __libc_pututline (const struct utmp *data) found = false; } else - found = __utmp_equal (&last_entry, data); + found = matches_last_entry (data); } if (!found)