From 10e1ddaa45634aa3e90d20a5b41695731f5d249b Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 20:08:10 +0800 Subject: [PATCH 01/12] Instruct git to ignore generated files Avoids accidentally committing them. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58b171c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +ideviceunback From 102366eeaf678f0da7e8dd52afa8fe424322fde6 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:22:35 +0800 Subject: [PATCH 02/12] Remove trailing whitespace Suggested-by: nano & git --- ideviceunback.c | 146 ++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/ideviceunback.c b/ideviceunback.c index bbcca04..4102743 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -7,16 +7,16 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation + to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the Software + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * - */ + */ #include #include @@ -55,7 +55,7 @@ struct manrec { char domain[1024]; char filepath[1024]; char abspath[1024]; - char digest[1024]; + char digest[1024]; char enckey[1024]; char shain[2048]; uint16_t mode; @@ -87,11 +87,11 @@ char help[]="ideviceunback [-i ] [-o ] [-v] [-q] [-h] [ Function Name : filecopy Returns Type : int ----Parameter List - 1. char *source, - 2. char *dest , + 1. char *source, + 2. char *dest , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -101,7 +101,7 @@ char help[]="ideviceunback [-i ] [-o ] [-v] [-q] [-h] [ \------------------------------------------------------------------*/ int filecopy( char *source, char *dest ) { - static char buffer[4096]; + static char buffer[4096]; FILE *s, *d; size_t rsize, wsize; @@ -144,11 +144,11 @@ int filecopy( char *source, char *dest ) Function Name : mkdirp Returns Type : int ----Parameter List - 1. char *path, - 2. int mode , + 1. char *path, + 2. int mode , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -212,10 +212,10 @@ int mkdirp( char *path, int mode ) Function Name : *splitpath Returns Type : char ----Parameter List - 1. char *fullpath , + 1. char *fullpath , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -244,11 +244,11 @@ char *splitpath( char *fullpath ) { Function Name : readstr Returns Type : int ----Parameter List - 1. char **p, - 2. char *buf , + 1. char **p, + 2. char *buf , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -284,8 +284,8 @@ int readstr( char **p, char *buf, size_t buf_sz ) { if ((*bp & mask[2]) == mask[2]) i++; if (buf_sz) { - *buf = *bp; - buf++; + *buf = *bp; + buf++; buf_sz--; } @@ -303,11 +303,11 @@ int readstr( char **p, char *buf, size_t buf_sz ) { Function Name : readuint8 Returns Type : int ----Parameter List - 1. char **p, - 2. uint8_t *i , + 1. char **p, + 2. uint8_t *i , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -327,11 +327,11 @@ int readuint8( char **p, uint8_t *i ) { Function Name : readuint16 Returns Type : int ----Parameter List - 1. char **p, - 2. uint16_t *i , + 1. char **p, + 2. uint16_t *i , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -353,11 +353,11 @@ int readuint16( char **p, uint16_t *i ) { Function Name : readuint32 Returns Type : int ----Parameter List - 1. char **p, - 2. uint32_t *i , + 1. char **p, + 2. uint32_t *i , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -370,10 +370,10 @@ int readuint32( char **p, uint32_t *i ) { memcpy(&a, *p, sizeof(uint32_t)); *i = ( - ((a & 0x000000FFUL) << 24) | - ((a & 0x0000FF00UL) << 8) | - ((a & 0x00FF0000UL) >> 8) | - ((a & 0xFF000000UL) >> 24) + ((a & 0x000000FFUL) << 24) | + ((a & 0x0000FF00UL) << 8) | + ((a & 0x00FF0000UL) >> 8) | + ((a & 0xFF000000UL) >> 24) ); (*p) += 4; @@ -385,11 +385,11 @@ int readuint32( char **p, uint32_t *i ) { Function Name : readuint64 Returns Type : int ----Parameter List - 1. char **p, - 2. uint64_t *i , + 1. char **p, + 2. uint64_t *i , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -402,13 +402,13 @@ int readuint64( char **p, uint64_t *i ) { uint64_t a = 0; memcpy(&a, *p, sizeof(uint64_t)); - *i = ((a & 0x00000000000000FFULL) << 56) | - ((a & 0x000000000000FF00ULL) << 40) | - ((a & 0x0000000000FF0000ULL) << 24) | - ((a & 0x00000000FF000000ULL) << 8) | - ((a & 0x000000FF00000000ULL) >> 8) | - ((a & 0x0000FF0000000000ULL) >> 24) | - ((a & 0x00FF000000000000ULL) >> 40) | + *i = ((a & 0x00000000000000FFULL) << 56) | + ((a & 0x000000000000FF00ULL) << 40) | + ((a & 0x0000000000FF0000ULL) << 24) | + ((a & 0x00000000FF000000ULL) << 8) | + ((a & 0x000000FF00000000ULL) >> 8) | + ((a & 0x0000FF0000000000ULL) >> 24) | + ((a & 0x00FF000000000000ULL) >> 40) | ((a & 0xFF00000000000000ULL) >> 56); (*p) += 8; return 0; @@ -420,12 +420,12 @@ int readuint64( char **p, uint64_t *i ) { Function Name : parse_parameters Returns Type : int ----Parameter List - 1. struct globals *g, - 2. int argc, - 3. char **argv , + 1. struct globals *g, + 2. int argc, + 3. char **argv , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -473,10 +473,10 @@ int parse_parameters( struct globals *g, int argc, char **argv ) { Function Name : manifest_pre10_decode Returns Type : int ----Parameter List - 1. void, + 1. void, ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -500,7 +500,7 @@ int manifest_pre10_decode( struct globals *g ) { } /* - * Get manifest filesize so we can mmap the whole file + * Get manifest filesize so we can mmap the whole file */ if (fstat(fd, &sb) == -1) { fprintf(stderr,"Cannot stat '%s' (%s)\n", g->manifest_filename, strerror(errno)); @@ -602,7 +602,7 @@ int manifest_pre10_decode( struct globals *g ) { } /* - * Final interpretation of the decoded manifest item and + * Final interpretation of the decoded manifest item and * deciding what to do with it. */ if ((m.mode & 0xE000)==0x8000) { @@ -624,7 +624,7 @@ int manifest_pre10_decode( struct globals *g ) { } else { filecopy( g->hashfn, newpath); if (!g->quiet) fprintf(stdout, " copied"); - } + } } } if (!g->quiet) fprintf(stdout,"\n"); @@ -651,13 +651,13 @@ int manifest_pre10_decode( struct globals *g ) { Function Name : int Returns Type : static ----Parameter List - 1. sqlite3_callback( void *NotUsed, - 2. int argc, - 3. char **argv, - 4. char **azColName , + 1. sqlite3_callback( void *NotUsed, + 2. int argc, + 3. char **argv, + 4. char **azColName , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -717,10 +717,10 @@ static int sq3_callback( void *NotUsed, int argc, char **argv, char **azColName Function Name : manifest_sqlite3_decode Returns Type : int ----Parameter List - 1. struct globals *g , + 1. struct globals *g , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -759,11 +759,11 @@ int manifest_sqlite3_decode( struct globals *g ) { Function Name : main Returns Type : int ----Parameter List - 1. int argc, - 2. char **argv , + 1. int argc, + 2. char **argv , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: From c42aac7567a56d14a9e60e6e9e3e8f5948b83a00 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:59:25 +0800 Subject: [PATCH 03/12] Add whitespace before equals in Makefile for readability --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f25ae10..867dc48 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -CFLAGS= -Wall -g -OBJS= sha1.o -LDLIBS= -lsqlite3 +CFLAGS = -Wall -g +OBJS = sha1.o +LDLIBS = -lsqlite3 all: ideviceunback From bd187f430685b0e384a5cf9e6f332d4393b916ff Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:03:21 +0800 Subject: [PATCH 04/12] Reduce scope of variables Suggested-by: cppcheck --- ideviceunback.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ideviceunback.c b/ideviceunback.c index 4102743..4d68376 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -224,10 +224,8 @@ int mkdirp( char *path, int mode ) \------------------------------------------------------------------*/ char *splitpath( char *fullpath ) { - char *p; - if (fullpath) { - p = strrchr(fullpath, '/'); + char *p = strrchr(fullpath, '/'); if (p) { *p = '\0'; p++; @@ -258,7 +256,6 @@ char *splitpath( char *fullpath ) { \------------------------------------------------------------------*/ int readstr( char **p, char *buf, size_t buf_sz ) { static unsigned short mask[] = {192, 224, 240}; // UTF8 size detect mask - uint8_t i; uint8_t slr[2]; uint16_t sl; char *bp = *p; @@ -278,7 +275,7 @@ int readstr( char **p, char *buf, size_t buf_sz ) { if (buf) { while ((*bp != '\0')&&(sl--)) { - i = 0; + uint8_t i = 0; if ((*bp & mask[0]) == mask[0]) i++; if ((*bp & mask[1]) == mask[1]) i++; if ((*bp & mask[2]) == mask[2]) i++; @@ -609,12 +606,11 @@ int manifest_pre10_decode( struct globals *g ) { snprintf(g->hashfn, sizeof(g->hashfn), "%s/%s", g->inputpath, m.hashstr); if( access( g->hashfn, F_OK ) != -1 ) { char newpath[PATH_MAX]; - char *fn; if (g->verbose) fprintf(stdout,"\n"); if (!g->quiet) fprintf(stdout,"FILE: %s =(exists)=> %s", g->hashfn, m.filepath); snprintf(newpath, sizeof(newpath),"%s/%s", g->outputpath, m.filepath); if (g->decode_only == 0) { - fn = splitpath(newpath); + char *fn = splitpath(newpath); if (fn) { mkdirp( newpath, S_IRWXU ); *(fn -1) = '/'; @@ -682,11 +678,10 @@ static int sq3_callback( void *NotUsed, int argc, char **argv, char **azColName snprintf(g.hashfn, sizeof(g.hashfn), "%s/%c%c/%s", g.inputpath, fileID[0], fileID[1], fileID); if( access( g.hashfn, F_OK ) != -1 ) { char newpath[PATH_MAX]; - char *fn; if (!g.quiet) fprintf(stdout,"FILE: %s =(exists)=> %s", g.hashfn, relativePath); snprintf(newpath, sizeof(newpath),"%s/%s", g.outputpath, relativePath); if (g.decode_only == 0) { - fn = splitpath(newpath); + char *fn = splitpath(newpath); if (fn) { mkdirp( newpath, S_IRWXU ); *(fn -1) = '/'; From eb9edfea70d46e0e252a556c9f71463f23f6bb7e Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:14:22 +0800 Subject: [PATCH 05/12] Declare unmodified variable as const Suggested-by: cppcheck --- ideviceunback.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ideviceunback.c b/ideviceunback.c index 4d68376..1a62f0d 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -255,7 +255,7 @@ char *splitpath( char *fullpath ) { \------------------------------------------------------------------*/ int readstr( char **p, char *buf, size_t buf_sz ) { - static unsigned short mask[] = {192, 224, 240}; // UTF8 size detect mask + static const unsigned short mask[] = {192, 224, 240}; // UTF8 size detect mask uint8_t slr[2]; uint16_t sl; char *bp = *p; From 67b107975bc94ed9c85045b52144c7f01f27e54b Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:16:33 +0800 Subject: [PATCH 06/12] Use printf length modifier z for size_t arguments It will always be the right size. Suggested-by: cppcheck --- ideviceunback.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ideviceunback.c b/ideviceunback.c index 1a62f0d..ed84659 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -126,7 +126,7 @@ int filecopy( char *source, char *dest ) wsize = fwrite( buffer, 1, rsize, d ); if ( rsize != wsize ) { - fprintf(stderr,"WARNING: Read '%lu' bytes, but only could write '%lu'", rsize, wsize ); + fprintf(stderr,"WARNING: Read '%zu' bytes, but only could write '%zu'", rsize, wsize ); } } } while ( rsize > 0 ); From 471d3d16fea0e1cc23fb0abd14715461bcb60594 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:20:30 +0800 Subject: [PATCH 07/12] Use PRIu*/PRIx* macros for printf length modifiers for uintN_t types Suggested-by: cppcheck --- ideviceunback.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ideviceunback.c b/ideviceunback.c index ed84659..97e0586 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -560,7 +561,7 @@ int manifest_pre10_decode( struct globals *g ) { , m.mode & 0x1 ? 'x' : '-' ); - fprintf(stdout,"|%lu|uid:%u gid:%u|Times(%u,%u,%u)|Size:%ld bytes|Flags:%02x|Numprops:%u" + fprintf(stdout,"|%" PRIu64 "|uid:%" PRIu32 " gid:%" PRIu32 "|Times(%" PRIu32 ",%" PRIu32 ",%" PRIu32 ")|Size:%" PRIu64 " bytes|Flags:%02" PRIx8 "|Numprops:%" PRIu8 , m.inode , m.userid , m.groupid From a9b34b47a57e8e898fa7bf1cfb5aa16310daf005 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 19:02:28 +0800 Subject: [PATCH 08/12] Close open file before returning from function with error Suggested-by: cppcheck --- ideviceunback.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ideviceunback.c b/ideviceunback.c index 97e0586..5847998 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -117,6 +117,7 @@ int filecopy( char *source, char *dest ) if (!d) { fprintf(stderr,"ERROR: Cannot open '%s' for writing (%s).", dest, strerror(errno) ); + fclose(s); return -1; } From 4593582274cf660a805e900fcf2f131714706211 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Thu, 6 Jan 2022 15:40:05 +0800 Subject: [PATCH 09/12] Mark all/clean/install make targets as phony They don't create files so should be marked this way in case someone accidentally creates files named them, which then blocks execution of the targets. --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index 867dc48..e0a282a 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ CFLAGS = -Wall -g OBJS = sha1.o LDLIBS = -lsqlite3 +.PHONY: all clean install + all: ideviceunback ideviceunback: $(OBJS) ideviceunback.c From 1f1f708b05f2f9af4dcf446b3c25bb34f06d1e5e Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 20:03:40 +0800 Subject: [PATCH 10/12] Make the installation location configurable at build time Support the traditional DESTDIR, PREFIX, and BINDIR variables, as well as INSTALLDIR for just installing to a specific directory. This will be useful for creating packages of ideviceunback, or installing ideviceunback into a home directory or other path. --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e0a282a..3eb7287 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +PREFIX = /usr/local +BINDIR = $(PREFIX)/bin +INSTALLDIR = $(DESTDIR)$(BINDIR) CFLAGS = -Wall -g OBJS = sha1.o LDLIBS = -lsqlite3 @@ -12,4 +15,5 @@ clean: $(RM) ideviceunback *.o install: ideviceunback - install ideviceunback /usr/local/bin + mkdir -p $(INSTALLDIR) + install ideviceunback $(INSTALLDIR) From 5ac53f67ecc8efdb6f5b8001e2f44380da6cda35 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 20:07:02 +0800 Subject: [PATCH 11/12] Append to CFLAGS instead of overriding them Allows redistributors to add hardening and other flags. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3eb7287..b0c3266 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ PREFIX = /usr/local BINDIR = $(PREFIX)/bin INSTALLDIR = $(DESTDIR)$(BINDIR) -CFLAGS = -Wall -g +CFLAGS += -Wall -g OBJS = sha1.o LDLIBS = -lsqlite3 From 7db683f12b2b344c23b35a7a14fa7e7931867a9b Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Wed, 22 Dec 2021 20:23:05 +0800 Subject: [PATCH 12/12] Handle errors when creating symlinks Suggested-by: gcc -O1 -D_FORTIFY_SOURCE=2 --- ideviceunback.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ideviceunback.c b/ideviceunback.c index 5847998..8a3b0c2 100644 --- a/ideviceunback.c +++ b/ideviceunback.c @@ -617,8 +617,10 @@ int manifest_pre10_decode( struct globals *g ) { mkdirp( newpath, S_IRWXU ); *(fn -1) = '/'; if (g->linkonly) { - link( g->hashfn, newpath ); - if (!g->quiet) fprintf(stdout, " linked"); + if (link( g->hashfn, newpath ) == -1) { + fprintf(stderr, "ERROR: link('%s', '%s'); '%s'", g->hashfn, newpath, strerror(errno)); + if (!g->quiet) fprintf(stdout, " not linked"); + } else if (!g->quiet) fprintf(stdout, " linked"); } else { filecopy( g->hashfn, newpath); if (!g->quiet) fprintf(stdout, " copied"); @@ -688,8 +690,10 @@ static int sq3_callback( void *NotUsed, int argc, char **argv, char **azColName mkdirp( newpath, S_IRWXU ); *(fn -1) = '/'; if (g.linkonly) { - link( g.hashfn, newpath ); - if (!g.quiet) fprintf(stdout, " linked"); + if (link( g.hashfn, newpath ) == -1) { + fprintf(stderr, "ERROR: link('%s', '%s'); '%s'", g.hashfn, newpath, strerror(errno)); + if (!g.quiet) fprintf(stdout, " not linked"); + } else if (!g.quiet) fprintf(stdout, " linked"); } else { filecopy( g.hashfn, newpath); if (!g.quiet) fprintf(stdout, " copied");