apk3: lots of endianness issues
Attempting to use apk3 packages on a big endian system will end in failure because of many places; these are the ones I could find so far:
adb.c:
-
.magic = ADB_FORMAT_MAGIC,
->.magic = htole32(ADB_FORMAT_MAGIC),
app_mkpkg.c:
@@ -195,12 +195,12 @@ static int mkpkg_process_dirent(void *pctx, int dirfd, const char *entry)
case S_IFBLK:
case S_IFCHR:
case S_IFIFO:
- ft.dev.mode = fi.mode & S_IFMT;
- ft.dev.dev = fi.device;
+ ft.dev.mode = htole16(fi.mode & S_IFMT);
+ ft.dev.dev = htole64(fi.device);
target = APK_BLOB_STRUCT(ft.dev);
break;
case S_IFLNK:
- ft.symlink.mode = fi.mode & S_IFMT;
+ ft.symlink.mode = htole16(fi.mode & S_IFMT);
r = readlinkat(dirfd, entry, ft.symlink.target, sizeof ft.symlink.target);
if (r < 0) return r;
target = APK_BLOB_PTR_LEN((void*)&ft.symlink, sizeof(ft.symlink.mode) + r);
@@ -385,8 +385,8 @@ static int mkpkg_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *a
if (!APK_BLOB_IS_NULL(target)) continue;
if (!sz) continue;
struct adb_data_package hdr = {
- .path_idx = i,
- .file_idx = j,
+ .path_idx = htole32(i),
+ .file_idx = htole32(j),
};
int n = apk_pathbuilder_pushb(&ctx->pb, filename);
adb_c_block_data(
extract_v3.c:
-
mode = *(uint16_t*)target.ptr;
->mode = le16toh(*(uint16_t*)target.ptr);
-
fi.device = ((struct unaligned64 *)target.ptr)->value;
->fi.device = le64toh(((struct unaligned64 *)target.ptr)->value);
-
hdr->path_idx != ctx->cur_path
->le32toh(hdr->path_idx) != ctx->cur_path
-
hdr->file_idx != ctx->cur_file
->le32toh(hdr->file_idx) != ctx->cur_file
After this, I could get packages to extract (but the changes may not be exhaustive). However, signature verification still fails, and I have no idea why. Big endian-created packages will pass verification (and fail it on LE) while little-endian-created packages will fail verification (and pass on LE). However, the message digest and stuff appears to be identical whether running on LE or on BE.
Also, mkpkg
will still create packages whose data differ when run on systems of different endianness.