From patchwork Fri Jul 20 02:57:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 946694 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="gpgG+0Xi"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 41Wwbl6Tltz9s4V for ; Fri, 20 Jul 2018 13:00:03 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 9A3C4C21E42; Fri, 20 Jul 2018 02:58:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 323C3C21C4A; Fri, 20 Jul 2018 02:58:39 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id EE521C21DC1; Fri, 20 Jul 2018 02:57:23 +0000 (UTC) Received: from mail-pl0-f65.google.com (mail-pl0-f65.google.com [209.85.160.65]) by lists.denx.de (Postfix) with ESMTPS id 4BE55C21DED for ; Fri, 20 Jul 2018 02:57:19 +0000 (UTC) Received: by mail-pl0-f65.google.com with SMTP id t17-v6so1955144ply.13 for ; Thu, 19 Jul 2018 19:57:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gdSLz1Ry8iefhwZaQbECAt0igIjvg9SBGP9hyNj9E8Q=; b=gpgG+0Xi8D9CIAJe4XKorNd0IXHzcawGy+S/GWXrmbmOC3jI6t741FUfNl4BlWngGd NKokgcQ2zPlrMhyS9oCpFBHbUMWUQOGpaF6+QRTPXkX1Pi3uDtFoEEJJwYlTEoH+t7z/ 4H1Jb8Uyd2xTEaQnxU7WYEJMtUQQEYt/9is7Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gdSLz1Ry8iefhwZaQbECAt0igIjvg9SBGP9hyNj9E8Q=; b=pxQ+mN61QOj/jUlYt8Rm0aTQP1FsbBfZpGMgTIW1v+G1fsGk9/s7xGFKphGphXV1UW eGQkm7kHOVMuIxeSxehDt4pfVJLZui0dTHx/4lMaE3AuazXdejdd3LjSXIDXLBDXE8In +tJvEnF/NojA8Y/1XMgCKS7fYzvDlJSiGDHlcIEUC5K2aTW2CZNwugf4ml5BMLVfgTEQ FOB1RnWud0xT+P3xNLHCoWW4+SXv7BEtxBBm1RwM+w4TZ+wsHY15M46hPbd5qPGikmCI Ckd5v4qoob5hK2puLhtdIj1pU6M/9ajmT2LU/YYzRvR8/LJ+ruD59sCSG7O2/mjIx1Qq 7VSg== X-Gm-Message-State: AOUpUlH33nnyz4lP6Ul7+Kww+kxv/FBhlvhMxTfb+3WATgbaa++jiNGP oWGU/rIBzWcAFqR6R5DOOP/zsA== X-Google-Smtp-Source: AAOMgpe3BZQTf0H48ZR7df5loJlNKtFpM1OfPcQOPRaXGk7xlsfW9AXNhnc6S8527PXgA9DeJ1dvrw== X-Received: by 2002:a17:902:864b:: with SMTP id y11-v6mr262262plt.335.1532055437964; Thu, 19 Jul 2018 19:57:17 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id c19-v6sm570869pfn.182.2018.07.19.19.57.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Jul 2018 19:57:17 -0700 (PDT) From: AKASHI Takahiro To: trini@konsulko.com Date: Fri, 20 Jul 2018 11:57:11 +0900 Message-Id: <20180720025723.6736-6-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180720025723.6736-1-takahiro.akashi@linaro.org> References: <20180720025723.6736-1-takahiro.akashi@linaro.org> Cc: u-boot@lists.denx.de, xypron.glpk@gmx.de, agraf@suse.de Subject: [U-Boot] [PATCH 05/17] fs: fat: check and normailze file name X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" FAT file system's long file name support is a bit complicated and has some restrictions on its naming. We should be careful about it especially for write as it may easily end up with wrong file system. normalize_longname() check for the rules and normalize a file name if necessary. Please note, however, that this function is yet to be extended to fully comply with the standard. Signed-off-by: AKASHI Takahiro --- fs/fat/fat_write.c | 52 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 3b77557b3e..6c715a70f4 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -899,6 +899,44 @@ static dir_entry *find_directory_entry(fsdata *mydata, int startsect, return NULL; } +static int normalize_longname(char *l_filename, const char *filename) +{ + const char *p, legal[] = "!#$%&\'()-.@^`_{}~"; + char c; + int name_len; + + /* Check that the filename is valid */ + for (p = filename; p < filename + strlen(filename); p++) { + c = *p; + + if (('0' <= c) && (c <= '9')) + continue; + if (('A' <= c) && (c <= 'Z')) + continue; + if (('a' <= c) && (c <= 'z')) + continue; + if (strchr(legal, c)) + continue; + /* extended code */ + if ((0x80 <= c) && (c <= 0xff)) + continue; + + return -1; + } + + /* Normalize it */ + name_len = strlen(filename); + if (name_len >= VFAT_MAXLEN_BYTES) + /* should return an error? */ + name_len = VFAT_MAXLEN_BYTES - 1; + + memcpy(l_filename, filename, name_len); + l_filename[name_len] = 0; /* terminate the string */ + downcase(l_filename, INT_MAX); + + return 0; +} + static int do_fat_write(const char *filename, void *buffer, loff_t size, loff_t *actwrite) { @@ -910,7 +948,7 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size, fsdata datablock; fsdata *mydata = &datablock; int cursect; - int ret = -1, name_len; + int ret = -1; char l_filename[VFAT_MAXLEN_BYTES]; *actwrite = size; @@ -971,13 +1009,11 @@ static int do_fat_write(const char *filename, void *buffer, loff_t size, } dentptr = (dir_entry *) do_fat_read_at_block; - name_len = strlen(filename); - if (name_len >= VFAT_MAXLEN_BYTES) - name_len = VFAT_MAXLEN_BYTES - 1; - - memcpy(l_filename, filename, name_len); - l_filename[name_len] = 0; /* terminate the string */ - downcase(l_filename, INT_MAX); + if (normalize_longname(l_filename, filename)) { + printf("FAT: illegal filename (%s)\n", filename); + ret = -EINVAL; + goto exit; + } startsect = mydata->rootdir_sect; retdent = find_directory_entry(mydata, startsect,