Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178316516
D62214.1796488121.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
D62214.1796488121.diff
View Options
Index: usr.sbin/fstyp/apfs.c
===================================================================
--- usr.sbin/fstyp/apfs.c
+++ usr.sbin/fstyp/apfs.c
@@ -2,6 +2,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>. All rights reserved.
+ * Copyright (c) 2026 Xiangbo Kong <yarshure@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -26,10 +27,9 @@
*/
#include <sys/cdefs.h>
-#include <assert.h>
-#include <err.h>
-#include <errno.h>
+#include <sys/endian.h>
#include <stdbool.h>
+#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -44,24 +44,26 @@
* https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf
*/
-#define NX_CKSUM_SZ 8
+#define MAX_CKSUM_SIZE 8
-typedef uint64_t nx_oid_t;
+typedef uint64_t oid_t;
+typedef uint64_t xid_t;
+typedef int64_t paddr_t;
-typedef uint64_t nx_xid_t;
-
-struct nx_obj {
- uint8_t o_cksum[NX_CKSUM_SZ]; /* Fletcher 64 */
- nx_oid_t o_oid;
- nx_xid_t o_xid;
+struct obj_phys {
+ uint8_t o_cksum[MAX_CKSUM_SIZE]; /* Fletcher 64 */
+ oid_t o_oid;
+ xid_t o_xid;
uint32_t o_type;
uint32_t o_subtype;
-};
+} __packed;
+
+_Static_assert(sizeof(struct obj_phys) == 32, "obj_phys size");
-/* nx_obj::o_oid */
-#define OID_NX_SUPERBLOCK 1
+/* obj_phys::o_oid */
+#define OID_NX_SUPERBLOCK 1
-/* nx_obj::o_type: */
+/* obj_phys::o_type */
#define OBJECT_TYPE_MASK 0x0000ffff
#define OBJECT_TYPE_NX_SUPERBLOCK 0x00000001
#define OBJECT_TYPE_FLAGS_MASK 0xffff0000
@@ -74,27 +76,89 @@
#define OBJ_ENCRYPTED 0x10000000
#define OBJ_NONPERSISTENT 0x08000000
+#define NX_MAGIC 0x4253584e /* "NXSB" */
+#define NX_MINIMUM_BLOCK_SIZE 4096
+#define NX_MAXIMUM_BLOCK_SIZE 65536
+
struct nx_superblock {
- struct nx_obj nx_o;
- char nx_magic[4];
- /* ... other stuff that doesn't matter */
-};
+ struct obj_phys nx_o;
+ uint32_t nx_magic;
+ uint32_t nx_block_size;
+ /* ... remaining fields follow */
+} __packed;
+
+_Static_assert(offsetof(struct nx_superblock, nx_magic) == 32,
+ "nx_magic offset");
+_Static_assert(offsetof(struct nx_superblock, nx_block_size) == 36,
+ "nx_block_size offset");
+
+/*
+ * Compute the APFS Fletcher-64 checksum over everything after o_cksum.
+ */
+static uint64_t
+apfs_fletcher64(const void *buf, size_t len)
+{
+ const uint8_t *b = buf;
+ uint64_t sum1, sum2, check1, check2;
+ size_t off;
+
+ sum1 = sum2 = 0;
+ for (off = MAX_CKSUM_SIZE; off + sizeof(uint32_t) <= len;
+ off += sizeof(uint32_t)) {
+ sum1 = (sum1 + le32dec(b + off)) % 0xffffffffULL;
+ sum2 = (sum2 + sum1) % 0xffffffffULL;
+ }
+ check1 = 0xffffffffULL - ((sum1 + sum2) % 0xffffffffULL);
+ check2 = 0xffffffffULL - ((sum1 + check1) % 0xffffffffULL);
+ return ((check2 << 32) | check1);
+}
+
+static bool
+apfs_obj_cksum_valid(const struct obj_phys *obj, size_t len)
+{
+
+ return (le64dec(obj->o_cksum) == apfs_fletcher64(obj, len));
+}
+
+static bool
+apfs_is_nxsb(const struct nx_superblock *nx)
+{
+
+ return (le32toh(nx->nx_magic) == NX_MAGIC &&
+ le64toh(nx->nx_o.o_oid) == OID_NX_SUPERBLOCK &&
+ (le32toh(nx->nx_o.o_type) & OBJECT_TYPE_MASK) ==
+ OBJECT_TYPE_NX_SUPERBLOCK);
+}
int
fstyp_apfs(FILE *fp, char *label, size_t size)
{
struct nx_superblock *csb;
+ uint32_t block_size;
int retval;
retval = 1;
- csb = read_buf(fp, 0, sizeof(*csb));
+ csb = read_buf(fp, 0, NX_MINIMUM_BLOCK_SIZE);
if (csb == NULL)
goto fail;
- /* Ideally, checksum the SB here. */
- if (strncmp(csb->nx_magic, "NXSB", 4) != 0 ||
- csb->nx_o.o_oid != OID_NX_SUPERBLOCK ||
- (csb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK)
+ if (!apfs_is_nxsb(csb))
+ goto fail;
+
+ block_size = le32toh(csb->nx_block_size);
+ if (block_size < NX_MINIMUM_BLOCK_SIZE ||
+ block_size > NX_MAXIMUM_BLOCK_SIZE ||
+ (block_size & (block_size - 1)) != 0)
+ goto fail;
+
+ if (block_size != NX_MINIMUM_BLOCK_SIZE) {
+ free(csb);
+ csb = read_buf(fp, 0, block_size);
+ if (csb == NULL)
+ goto fail;
+ }
+
+ if (!apfs_obj_cksum_valid(&csb->nx_o, block_size))
goto fail;
retval = 0;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 5, 4:28 PM (18 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42235123
Default Alt Text
D62214.1796488121.diff (4 KB)
Attached To
Mode
D62214: fstyp: verify APFS superblock Fletcher-64 checksum
Attached
Detach File
Event Timeline