From nobody Thu Oct 10 08:40:42 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XPNWG2TJJz5Yg7h; Thu, 10 Oct 2024 08:40:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XPNWG1vg3z4jdn; Thu, 10 Oct 2024 08:40:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728549642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FcEXvquua37mDylCRIq9EeUNEplxd5gC/iYe1QiOEkk=; b=S47mGJMHM9xVh5Cf0wlI8dHOFwn+YtjqtETbIF4ivD8smLJCOTXn9ysNKfqEyS4wPXj8Rn vz3L+rI2v6tK0YE6l054U2SjmiQ6NbuYJepgs0hxO9VIhxOgvnYlEj988QexCEuYe6Hitx c5KTffyP7HVXcmeUQvIeaDPcV14F89u9FMDFOomBk3pWSE43mSMzgMh06qQVYeIQy5DZaR Qcus2W9qYFrq2Eie4RHoRA6UzHrA2vWjd8rIhti5j+wxASBnSrEHsQQr+tvLaeSSwSHCXp 4Rrccsr3yqaFwhIX1VSOMvedrBXAH4SUtK9CLsUbx1Au0vt0D7O3Syue6n1GRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728549642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=FcEXvquua37mDylCRIq9EeUNEplxd5gC/iYe1QiOEkk=; b=X9/VVBSiLb5u7ZrZdkD4jc4HQ5ffKXkmqcTg2o+w0m5naz2WKes33xD+OXcD53+q2m2j76 0ra1bdEfDDhbQR9Qt1zZtgnmRLhrdgNdPc1bH7Cc2DY2cKnZfIIk3o8DnhdDNfcEEaB3eU THeVdRNdrpEaMVI3+0ODJ1OxIsklFZ2is6+k1bYXkSMDilZFWM+p3jJeO3CSOg/F6rYyP9 UlJEweXow7qRaU5c3JuHIXQnmYGTfqcIiA7IdWRoVzdHteC7qOjkbECBFNL/ffwD8pkbJQ V4XHYlFLdjbXa9SfywESe0sdvPYUFEdgS78bT9sT41t45gh/K0cAuiZYGNNLuw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728549642; a=rsa-sha256; cv=none; b=Gp1XNqtOyczGDBY1qkovlxdJj6TJfsGfpBECkakMXd5RzGwY1KOTtAZlNt84CCPHg2i7zQ p9niplolQeMXGDJfnpxzlb4mfjQTcgYPNlbO1dv4sHuFk3z+pLqXlIIaPktYBK6PYa6fvH x2fA7rokex6FoXNlM0yB2ENapELN1QZkVUP/qdUf7bCioVEd7TgyHm8bCopVNKOFkVwcg9 14LsfAr3LIw4NnvrXrx2ExwJEV8CdAEbCI9albJOKIqsTvtSX8ARQ/mzQQ8sIeYjpbxiii HWjU31Hxpvag94/pHXexGq+aCZR5n/0UqKepnFvJ6gBsOxIpPosUj3HUHJsmhw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XPNWG1Wg6zrHd; Thu, 10 Oct 2024 08:40:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49A8egg2009043; Thu, 10 Oct 2024 08:40:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49A8egNm009040; Thu, 10 Oct 2024 08:40:42 GMT (envelope-from git) Date: Thu, 10 Oct 2024 08:40:42 GMT Message-Id: <202410100840.49A8egNm009040@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Toomas Soome Subject: git: fae4b97c1a6b - main - loader: reading from large dosfs fails List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: tsoome X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fae4b97c1a6b762230824ac0ba169dff13fa006c Auto-Submitted: auto-generated The branch main has been updated by tsoome: URL: https://cgit.FreeBSD.org/src/commit/?id=fae4b97c1a6b762230824ac0ba169dff13fa006c commit fae4b97c1a6b762230824ac0ba169dff13fa006c Author: Toomas Soome AuthorDate: 2024-07-17 06:47:56 +0000 Commit: Toomas Soome CommitDate: 2024-10-10 08:40:25 +0000 loader: reading from large dosfs fails With 8GB disk image and FAT32, our read offset calculation wraps over 32-bit integer and we end up reading garbage. The problem appears when disk image is filled with data and the block to bytes translations do not fit into 32-bit integers. illumos issue: https://www.illumos.org/issues/16666 Sponsored by: MNX Cloud, Inc. MFC after: 1 week --- stand/libsa/dosfs.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/stand/libsa/dosfs.c b/stand/libsa/dosfs.c index 2ee95bf2c601..123350263d09 100644 --- a/stand/libsa/dosfs.c +++ b/stand/libsa/dosfs.c @@ -1,6 +1,7 @@ /* * Copyright (c) 1996, 1998 Robert Nordier * All rights reserved. + * Copyright 2024 MNX Cloud, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -139,8 +140,8 @@ static DOS_DE dot[2] = { #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus) /* Get start cluster from directory entry */ -#define stclus(sz, de) ((sz) != 32 ? cv2((de)->clus) : \ - ((u_int)cv2((de)->dex.h_clus) << 16) | \ +#define stclus(sz, de) ((sz) != 32 ? (u_int)cv2((de)->clus) : \ + ((u_int)cv2((de)->dex.h_clus) << 16) | \ cv2((de)->clus)) static int parsebs(DOS_FS *, DOS_BS *); @@ -152,7 +153,7 @@ static off_t fsize(DOS_FS *, DOS_DE *); static int fatcnt(DOS_FS *, u_int); static int fatget(DOS_FS *, u_int *); static int fatend(u_int, u_int); -static int ioread(DOS_FS *, u_int, void *, size_t); +static int ioread(DOS_FS *, uint64_t, void *, size_t); static int ioget(struct open_file *, daddr_t, void *, size_t); static int @@ -364,7 +365,9 @@ static int dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid) { off_t size; - u_int nb, off, clus, c, cnt, n; + uint64_t off; + size_t nb; + u_int clus, c, cnt, n; DOS_FILE *f = (DOS_FILE *)fd->f_fsdata; int err = 0; @@ -375,7 +378,7 @@ dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid) * 4-5 sec. */ twiddle(4); - nb = (u_int)nbyte; + nb = nbyte; if ((size = fsize(f->fs, &f->de)) == -1) return (EINVAL); if (nb > (n = size - f->offset)) @@ -402,8 +405,12 @@ dos_read(struct open_file *fd, void *buf, size_t nbyte, size_t *resid) } if (!clus || (n = f->fs->bsize - off) > cnt) n = cnt; - if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) : - secbyt(f->fs->lsndir)) + off, buf, n))) + if (c != 0) + off += blkoff(f->fs, (uint64_t)c); + else + off += secbyt(f->fs->lsndir); + err = ioread(f->fs, off, buf, n); + if (err != 0) goto out; f->offset += n; f->c = c; @@ -908,11 +915,12 @@ fatend(u_int sz, u_int c) * Offset-based I/O primitive */ static int -ioread(DOS_FS *fs, u_int offset, void *buf, size_t nbyte) +ioread(DOS_FS *fs, uint64_t offset, void *buf, size_t nbyte) { char *s; - u_int off, n; + size_t n; int err; + uint64_t off; u_char local_buf[SECSIZ]; s = buf;