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: 968433 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="XmsJmWQx"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 428bW06CgFz9rxp for ; Tue, 11 Sep 2018 17:04:12 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 5716EC21F48; Tue, 11 Sep 2018 07:02:01 +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-Google-Smtp-Source: ANB0VdZDHlewcZxvooAgvmNnWzpk13su7xyFHTKOmrvf9W3wXu7MhyhX6WXjPbwWol2FURFi9kdGsA== 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,