From patchwork Wed Dec 16 14:24:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 41465 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 B0688387090C; Wed, 16 Dec 2020 14:30:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0688387090C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1608129057; bh=Jzl4g54PToqxZNcEcHWOYwt/brXthsE/m3NLpnlQKqQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=eNuVKHsuFfdmCvmIybjR+Vb03Z2cZQZvpP1XkgeYPmlZ87C/c1CoXr4Ej9DePY9YD haWDpqUJ0YUikL4LZnucxsyEAdI2a0n7FyufFXu+geGnXwCQ6W73SuBq9b857TAztF t2Np/82ifErkDk6kl6i4diQPR5c2sGpMtTMbRVD8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dragonfly.birch.relay.mailchannels.net (dragonfly.birch.relay.mailchannels.net [23.83.209.51]) by sourceware.org (Postfix) with ESMTPS id F081C3854810 for ; Wed, 16 Dec 2020 14:30:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org F081C3854810 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 EE3C2322B86; Wed, 16 Dec 2020 14:24:27 +0000 (UTC) Received: from pdx1-sub0-mail-a30.g.dreamhost.com (100-105-161-17.trex.outbound.svc.cluster.local [100.105.161.17]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 6A2D2322AA7; Wed, 16 Dec 2020 14:24:27 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a30.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.18.11); Wed, 16 Dec 2020 14:24:27 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Hysterical-Illegal: 298341004f8755ba_1608128667665_1946763809 X-MC-Loop-Signature: 1608128667665:3986954298 X-MC-Ingress-Time: 1608128667665 Received: from pdx1-sub0-mail-a30.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a30.g.dreamhost.com (Postfix) with ESMTP id 14ACD7EED9; Wed, 16 Dec 2020 06:24:27 -0800 (PST) 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-a30.g.dreamhost.com (Postfix) with ESMTPSA id DABB17EED8; Wed, 16 Dec 2020 06:24:24 -0800 (PST) X-DH-BACKEND: pdx1-sub0-mail-a30 To: libc-alpha@sourceware.org Subject: [PATCH] getmntent: Generalize octal decoding Date: Wed, 16 Dec 2020 19:54:15 +0530 Message-Id: <20201216142415.173885-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-Spam-Status: No, score=-9.6 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_H2, 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 Cc: fweimer@redhat.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The current octal decoding looks for specific characters to decode. generalize this instead, allowing any arbitrary \xxx octal pattern to be decoded as long as it is a printable character. The decoding is now done a bit later so that it does not influence the "ignore" option of autofs, which is a hint for getmntent to skip over the line. With this change, "\151gnore" will not cuase the line to be skipped over, but the options string will show "ignore" correctly. Also add a test case to validate that encoded hashes get decoded correctly. This would be useful if/when the kernel starts escaping hashes in /proc/mounts. --- misc/mntent_r.c | 92 ++++++++++++++++++++++++---------------- misc/tst-mntent-autofs.c | 4 +- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/misc/mntent_r.c b/misc/mntent_r.c index b50ba0de89..ac56e40fd5 100644 --- a/misc/mntent_r.c +++ b/misc/mntent_r.c @@ -65,6 +65,27 @@ __endmntent (FILE *stream) libc_hidden_def (__endmntent) weak_alias (__endmntent, endmntent) +static char +parse_octal (char *s) +{ + char c = 0, t; + + t = s[0] - '0'; + if (t & (~0x7)) + return 0; + c |= t << 6; + + t = s[1] - '0'; + if (t & (~0x7)) + return 0; + c |= t << 3; + + t = s[2] - '0'; + if (t & (~0x7)) + return 0; + + return c | t; +} /* Since the values in a line are separated by spaces, a name cannot contain a space. Therefore some programs encode spaces in names @@ -77,43 +98,42 @@ decode_name (char *buf) char *wp = buf; do - if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0') - { - /* \040 is a SPACE. */ - *wp++ = ' '; - rp += 3; - } - else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1') - { - /* \011 is a TAB. */ - *wp++ = '\t'; - rp += 3; - } - else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2') - { - /* \012 is a NEWLINE. */ - *wp++ = '\n'; - rp += 3; - } - else if (rp[0] == '\\' && rp[1] == '\\') - { - /* We have to escape \\ to be able to represent all characters. */ - *wp++ = '\\'; - rp += 1; - } - else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4') - { - /* \134 is also \\. */ - *wp++ = '\\'; - rp += 3; - } - else + { + if (rp[0] == '\\') + { + char c; + + if ((c = parse_octal (&rp[1])) != 0) + { + *wp++ = c; + rp += 3; + continue; + } + if (rp[1] == '\\') + { + *wp++ = '\\'; + rp++; + continue; + } + } *wp++ = *rp; + } while (*rp++ != '\0'); return buf; } +static struct mntent * +decode_mntent_names (struct mntent *res) +{ + res->mnt_fsname = decode_name (res->mnt_fsname); + res->mnt_dir = decode_name (res->mnt_dir); + res->mnt_type = decode_name (res->mnt_type); + res->mnt_opts = decode_name (res->mnt_opts); + + return res; +} + static bool get_mnt_entry (FILE *stream, struct mntent *mp, char *buffer, int bufsiz) { @@ -151,19 +171,19 @@ get_mnt_entry (FILE *stream, struct mntent *mp, char *buffer, int bufsiz) while (head[0] == '\0' || head[0] == '#'); cp = __strsep (&head, " \t"); - mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) ""; + mp->mnt_fsname = cp != NULL ? cp : (char *) ""; if (head) head += strspn (head, " \t"); cp = __strsep (&head, " \t"); - mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) ""; + mp->mnt_dir = cp != NULL ? cp : (char *) ""; if (head) head += strspn (head, " \t"); cp = __strsep (&head, " \t"); - mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) ""; + mp->mnt_type = cp != NULL ? cp : (char *) ""; if (head) head += strspn (head, " \t"); cp = __strsep (&head, " \t"); - mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) ""; + mp->mnt_opts = cp != NULL ? cp : (char *) ""; switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0) { case 0: @@ -197,7 +217,7 @@ __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz) memset (mp, 0, sizeof (*mp)); else { - result = mp; + result = decode_mntent_names (mp); break; } } diff --git a/misc/tst-mntent-autofs.c b/misc/tst-mntent-autofs.c index e4c509f520..54b11b23ba 100644 --- a/misc/tst-mntent-autofs.c +++ b/misc/tst-mntent-autofs.c @@ -87,8 +87,8 @@ static struct test_case test_cases[] = /* These are not filtered because the string is escaped. '\151' is 'i', but it is not actually decoded by the parser. */ { "/etc/auto.\\151gnore /mnt/auto/16 autofs \\151gnore 0 0", - { "/etc/auto.\\151gnore", "/mnt/auto/16", "autofs", - "\\151gnore", } }, + { "/etc/auto.ignore", "/mnt/auto/16", "autofs", + "ignore", } }, }; static int