From patchwork Tue Sep 4 07:49:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 965805 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="a3hW/NU0"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 424K082TXdz9rvt for ; Tue, 4 Sep 2018 17:56:08 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 0110BC21EF1; Tue, 4 Sep 2018 07:53:36 +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 09BF9C21F05; Tue, 4 Sep 2018 07:52:42 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 10191C21F37; Tue, 4 Sep 2018 07:51:10 +0000 (UTC) Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by lists.denx.de (Postfix) with ESMTPS id 9B225C21EA2 for ; Tue, 4 Sep 2018 07:51:09 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id b129-v6so1232697pga.13 for ; Tue, 04 Sep 2018 00:51:09 -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=qNaJgioBe9gl2dduCmQpM6EPlZ0HJVibDPxhrX6FC3g=; b=a3hW/NU0n/L+vIlOhiuny/e0mXEyGxHzafHU2lEEvmJn3UmC8I/gbAgYqSOlk047Bl e7AiiNAWp8lHi6wgPEFe/kQ7CNg2m22mrr82Ua4yr8U80N5S9ZHJiYAspmeuI/hp0JGp hnUibIdH/npPCkWV9lVk/ziAUc9d6KxbAQEJE= 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=qNaJgioBe9gl2dduCmQpM6EPlZ0HJVibDPxhrX6FC3g=; b=YgZrEmwGgHHue72SxElZ8pwUPhwJLwXIlMU395UTfvbAC1tMsEtr5tHIBq5pppSuUB +/LDKCZ/u0IKKrlxAqFGNCW28WYMc+DgZcMmKs9PKvL6r2S6UjALQWJbI+lqBSm0TOAb R8XdKZFIr4Fok+GTqj/GgU+ZZX9GXWNCqSebKFbzO+Lv3vPWQ3Y6ky8RbnZ/DnadLvSj lg6hTuxd4tqP3zW3pyORutvJkm1obuL55H16Xme5Hzky8lJoFUXBViKoxIIZeGCjnYnE kc3bQMGxxNR9+22+dU905P1O30dhjDyDeQyl0RZgkE+/5ujolxdmrTXb6ZXcRJHkF+XD ah0w== X-Gm-Message-State: APzg51Ce3NgbLT2imhZ/5KHCMZ9IWRJfOpB3gUWsJZXCTf5Lx2Ah9nIi V90Mho7nzm+ZO9+xY5oPkukKHw== X-Google-Smtp-Source: ANB0Vdas7jMVrTN6d7+GsKuavKi6ACXYsVE4hpRcQ5k0z4ppMDcNqpZyXqhzsNqmIs2iGYGTiIdcVA== X-Received: by 2002:a63:fa0c:: with SMTP id y12-v6mr28138726pgh.177.1536047468309; Tue, 04 Sep 2018 00:51:08 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id n26-v6sm23409319pgv.78.2018.09.04.00.51.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Sep 2018 00:51:07 -0700 (PDT) From: AKASHI Takahiro To: trini@konsulko.com Date: Tue, 4 Sep 2018 16:49:32 +0900 Message-Id: <20180904074948.18146-8-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180904074948.18146-1-takahiro.akashi@linaro.org> References: <20180904074948.18146-1-takahiro.akashi@linaro.org> Cc: xypron.glpk@gmx.de, agraf@suse.de, u-boot@lists.denx.de Subject: [U-Boot] [PATCH v2 07/23] 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 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,