From patchwork Tue Sep 11 06:59:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 146407 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp3315465ljw; Tue, 11 Sep 2018 00:05:17 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbchYD3w09QLqrtiiipqOTCDmpvk+nUdxK83uQXmbVQ9Sg2acWXbQ6BC6XXk09Qy3iXP7kL X-Received: by 2002:a50:a9e2:: with SMTP id n89-v6mr26694566edc.158.1536649517735; Tue, 11 Sep 2018 00:05:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536649517; cv=none; d=google.com; s=arc-20160816; b=DQPcHP299dO4ql/IquTJJn9cFvWZMaZ9awaiptfQOq8R947OlfUaW16W6MIR3Vmyec bopriqYHbRZQYLjSj9YDHHJeinjUBQqXFpJJeudgSQNhdeqMP63AaQyov1ONgZYqNPM9 5aDnR4cEbLanxdEFjWqyguS2c29s97zq5IJsMK4cpqctFRohDgI8eY/twUugm23H6Pnv YuUM9Q1G4DNWCJHDiusi5tzAwDQmI/Q0Jw78NlN65RIDtzRHRBTYwVLyYwBUeBNq3MC+ Cg89IeR/eH2eb3Y0sNZi3VsX+LlG9vqQX3budLdTG3x2PGbFKMKkrkrHrVOsmdR8eL8e VCYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:message-id :date:to:from:dkim-signature; bh=gdXYslx8p5W/KMPvE1C04bKY7LPpHiqTmFibqJwhMZ8=; b=xQUaB9HHR5qdqT/nVOXkSPesGixKRIPivkZgKoBrVvXRIxZvre564Xr1ZOL6WoIRg8 BjVY0r/lplX+F5jUdMX72JHQTsa9EmzgiEeoaKeVkQloynR2oD9aBNbFjR1hc/tIUTyg BI5R5m8blHgt14mCcwNtkmxtrY8cM/lYkVjRxX6nudXXkCAs5uCXL57PM8TuOVMyfhTc YohszpFvdkU/Fxi+Qzog1TD1nz71ShMf2SFw6jf5CpAiewG9TJzFa3su86NEM5CkUp5b A3DrDOGFBQCaRTlMu3BvdFcfXFtJafQ+XySvubCXgUYR4wEpc70t36Ek9xn5rv5A579u 8nhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XmsJmWQx; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id t24-v6si2613920edq.93.2018.09.11.00.05.17; Tue, 11 Sep 2018 00:05:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=XmsJmWQx; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 0A37EC21EC8; Tue, 11 Sep 2018 07:02:00 +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 1ADA9C21EAE; Tue, 11 Sep 2018 06:59:52 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id AC18FC21EE4; Tue, 11 Sep 2018 06:59:31 +0000 (UTC) Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by lists.denx.de (Postfix) with ESMTPS id 98691C21E8A for ; Tue, 11 Sep 2018 06:59:26 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id d19-v6so11766532pgv.1 for ; Mon, 10 Sep 2018 23:59:26 -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=MVxMfgjxhQvMYNO4yw3uXrCa1n21Pbn66r3R2pzJQMs=; b=XmsJmWQx+qCbvt+Z7egcWg9tVQRxKo9iRmUxrHGxD7WM2f+T79BkoU7ZIJ+ZHtlJx3 3kcei+a5E/76ZuOT/zEXzMxx1K8rfTNJaxZxpM1C95Mr65IUqzDBMcr1f8D5fzg8wS9x dADfxcGsGfZMFlzPYg3DDi2Q0mABzV0wUeGzo= 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=MVxMfgjxhQvMYNO4yw3uXrCa1n21Pbn66r3R2pzJQMs=; b=WRBB6QDoisy7PiW5He/btHU6s/sbpx2ZK263M35hY+DsDFxv39E56WpsihsS5PcAyw jT3NQL5UZh8Fh7zSmgDHgxGJO7Dkno5+3lIHSXmD0PoTFrjsDaoxiviRR5By/+jPSEpQ 2iSv8nugS/2enuVJ8o7yWqifjxaDHehnb1L3U3lw9Ulzw+DqKnb5t3qmrObUkwG8VUeV TQW2CTe0AtGFQqrO+TNEgt83EYoQhPudruKzsEmRkMx/vyLKYcVyMJXhJ421Tg2fxoz6 Ep4WPaH9BNHJ8XB+9FEpVsFZcPK2JEE9Z1X4b2H7KXb4eIzKBnylsLOsW4tYqZKsTuEk D/GA== X-Gm-Message-State: APzg51B3kzUY+zUrMkyvxU0Ytzs2A9Yfu1elmuymgLjixp7qAjn8266C +2MUNVVPLK8I4syEIFoj5HhyaA== X-Received: by 2002:aa7:860b:: with SMTP id p11-v6mr24898263pfn.247.1536649165234; Mon, 10 Sep 2018 23:59:25 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id e14-v6sm22776940pff.128.2018.09.10.23.59.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Sep 2018 23:59:24 -0700 (PDT) From: "Akashi, Takahiro" To: trini@konsulko.com Date: Tue, 11 Sep 2018 15:59:02 +0900 Message-Id: <20180911065922.19141-7-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180911065922.19141-1-takahiro.akashi@linaro.org> References: <20180911065922.19141-1-takahiro.akashi@linaro.org> Cc: u-boot@lists.denx.de, xypron.glpk@gmx.de, agraf@suse.de Subject: [U-Boot] [PATCH v3 06/26] fs: fat: check and normalize 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" From: AKASHI Takahiro 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 3b77557b3ede..6c715a70f447 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,