bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 1998-2003 University of Illinois Board of Trustees |
| 3 | ** Copyright 1998-2003 Mark D. Roth |
| 4 | ** All rights reserved. |
| 5 | ** |
| 6 | ** block.c - libtar code to handle tar archive header blocks |
| 7 | ** |
| 8 | ** Mark D. Roth <roth@uiuc.edu> |
| 9 | ** Campus Information Technologies and Educational Services |
| 10 | ** University of Illinois at Urbana-Champaign |
| 11 | */ |
| 12 | |
| 13 | #include <internal.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 14 | #include <errno.h> |
| 15 | |
| 16 | #ifdef STDC_HEADERS |
| 17 | # include <string.h> |
| 18 | # include <stdlib.h> |
| 19 | #endif |
| 20 | |
DarthJabba9 | 247c68b | 2020-10-11 16:47:56 +0100 | [diff] [blame] | 21 | #ifdef TW_LIBTAR_DEBUG |
| 22 | #define DEBUG 1 |
| 23 | #endif |
| 24 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 25 | #ifdef USE_FSCRYPT |
| 26 | #include "fscrypt_policy.h" |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 27 | #endif |
| 28 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | #define BIT_ISSET(bitmask, bit) ((bitmask) & (bit)) |
| 30 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 31 | // Used to identify selinux_context in extended ('x') |
| 32 | // metadata. From RedHat implementation. |
| 33 | #define SELINUX_TAG "RHT.security.selinux=" |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 34 | #define SELINUX_TAG_LEN strlen(SELINUX_TAG) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 36 | // Used to identify fscrypt_policy in extended ('x') |
| 37 | #define FSCRYPT_TAG "TWRP.security.fscrypt=" |
| 38 | #define FSCRYPT_TAG_LEN strlen(FSCRYPT_TAG) |
| 39 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 40 | // Used to identify Posix capabilities in extended ('x') |
| 41 | #define CAPABILITIES_TAG "SCHILY.xattr.security.capability=" |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 42 | #define CAPABILITIES_TAG_LEN strlen(CAPABILITIES_TAG) |
| 43 | |
| 44 | // Used to identify Android user.default xattr in extended ('x') |
| 45 | #define ANDROID_USER_DEFAULT_TAG "ANDROID.user.default" |
| 46 | #define ANDROID_USER_DEFAULT_TAG_LEN strlen(ANDROID_USER_DEFAULT_TAG) |
| 47 | |
| 48 | // Used to identify Android user.inode_cache xattr in extended ('x') |
| 49 | #define ANDROID_USER_CACHE_TAG "ANDROID.user.inode_cache" |
| 50 | #define ANDROID_USER_CACHE_TAG_LEN strlen(ANDROID_USER_CACHE_TAG) |
| 51 | |
| 52 | // Used to identify Android user.inode_code_cache xattr in extended ('x') |
| 53 | #define ANDROID_USER_CODE_CACHE_TAG "ANDROID.user.inode_code_cache" |
| 54 | #define ANDROID_USER_CODE_CACHE_TAG_LEN strlen(ANDROID_USER_CODE_CACHE_TAG) |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 55 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 56 | /* read a header block */ |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 57 | /* FIXME: the return value of this function should match the return value |
| 58 | of tar_block_read(), which is a macro which references a prototype |
| 59 | that returns a ssize_t. So far, this is safe, since tar_block_read() |
| 60 | only ever reads 512 (T_BLOCKSIZE) bytes at a time, so any difference |
| 61 | in size of ssize_t and int is of negligible risk. BUT, if |
| 62 | T_BLOCKSIZE ever changes, or ever becomes a variable parameter |
| 63 | controllable by the user, all the code that calls it, |
| 64 | including this function and all code that calls it, should be |
| 65 | fixed for security reasons. |
| 66 | Thanks to Chris Palmer for the critique. |
| 67 | */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | int |
| 69 | th_read_internal(TAR *t) |
| 70 | { |
| 71 | int i; |
| 72 | int num_zero_blocks = 0; |
| 73 | |
| 74 | #ifdef DEBUG |
| 75 | printf("==> th_read_internal(TAR=\"%s\")\n", t->pathname); |
| 76 | #endif |
| 77 | |
| 78 | while ((i = tar_block_read(t, &(t->th_buf))) == T_BLOCKSIZE) |
| 79 | { |
| 80 | /* two all-zero blocks mark EOF */ |
| 81 | if (t->th_buf.name[0] == '\0') |
| 82 | { |
| 83 | num_zero_blocks++; |
| 84 | if (!BIT_ISSET(t->options, TAR_IGNORE_EOT) |
| 85 | && num_zero_blocks >= 2) |
| 86 | return 0; /* EOF */ |
| 87 | else |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | /* verify magic and version */ |
| 92 | if (BIT_ISSET(t->options, TAR_CHECK_MAGIC) |
| 93 | && strncmp(t->th_buf.magic, TMAGIC, TMAGLEN - 1) != 0) |
| 94 | { |
| 95 | #ifdef DEBUG |
| 96 | puts("!!! unknown magic value in tar header"); |
| 97 | #endif |
| 98 | return -2; |
| 99 | } |
| 100 | |
| 101 | if (BIT_ISSET(t->options, TAR_CHECK_VERSION) |
| 102 | && strncmp(t->th_buf.version, TVERSION, TVERSLEN) != 0) |
| 103 | { |
| 104 | #ifdef DEBUG |
| 105 | puts("!!! unknown version value in tar header"); |
| 106 | #endif |
| 107 | return -2; |
| 108 | } |
| 109 | |
| 110 | /* check chksum */ |
| 111 | if (!BIT_ISSET(t->options, TAR_IGNORE_CRC) |
| 112 | && !th_crc_ok(t)) |
| 113 | { |
| 114 | #ifdef DEBUG |
| 115 | puts("!!! tar header checksum error"); |
| 116 | #endif |
| 117 | return -2; |
| 118 | } |
| 119 | |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | #ifdef DEBUG |
| 124 | printf("<== th_read_internal(): returning %d\n", i); |
| 125 | #endif |
| 126 | return i; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* wrapper function for th_read_internal() to handle GNU extensions */ |
| 131 | int |
| 132 | th_read(TAR *t) |
| 133 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 134 | int i; |
| 135 | size_t sz, j, blocks; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 136 | char *ptr; |
| 137 | |
| 138 | #ifdef DEBUG |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 139 | printf("==> th_read(t=0x%p)\n", (void *)t); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | if (t->th_buf.gnu_longname != NULL) |
| 143 | free(t->th_buf.gnu_longname); |
| 144 | if (t->th_buf.gnu_longlink != NULL) |
| 145 | free(t->th_buf.gnu_longlink); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 146 | if (t->th_buf.selinux_context != NULL) |
| 147 | free(t->th_buf.selinux_context); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 148 | |
| 149 | #ifdef USE_FSCRYPT |
| 150 | if (t->th_buf.fep != NULL) |
| 151 | free(t->th_buf.fep); |
| 152 | #endif |
| 153 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 154 | if (t->th_buf.has_cap_data) |
| 155 | { |
| 156 | memset(&t->th_buf.cap_data, 0, sizeof(struct vfs_cap_data)); |
| 157 | t->th_buf.has_cap_data = 0; |
| 158 | } |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 159 | t->th_buf.has_user_default = 0; |
| 160 | t->th_buf.has_user_cache = 0; |
| 161 | t->th_buf.has_user_code_cache = 0; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 162 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 163 | memset(&(t->th_buf), 0, sizeof(struct tar_header)); |
| 164 | |
| 165 | i = th_read_internal(t); |
| 166 | if (i == 0) |
| 167 | return 1; |
| 168 | else if (i != T_BLOCKSIZE) |
| 169 | { |
| 170 | if (i != -1) |
| 171 | errno = EINVAL; |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | /* check for GNU long link extention */ |
| 176 | if (TH_ISLONGLINK(t)) |
| 177 | { |
| 178 | sz = th_get_size(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 179 | blocks = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0); |
| 180 | if (blocks > ((size_t)-1 / T_BLOCKSIZE)) |
| 181 | { |
| 182 | errno = E2BIG; |
| 183 | return -1; |
| 184 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 185 | #ifdef DEBUG |
| 186 | printf(" th_read(): GNU long linkname detected " |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 187 | "(%zu bytes, %zu blocks)\n", sz, blocks); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 188 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 189 | t->th_buf.gnu_longlink = (char *)malloc(blocks * T_BLOCKSIZE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 190 | if (t->th_buf.gnu_longlink == NULL) |
| 191 | return -1; |
| 192 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 193 | for (j = 0, ptr = t->th_buf.gnu_longlink; j < blocks; |
| 194 | j++, ptr += T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 195 | { |
| 196 | #ifdef DEBUG |
| 197 | printf(" th_read(): reading long linkname " |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 198 | "(%zu blocks left, ptr == %p)\n", blocks-j, (void *) ptr); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 199 | #endif |
| 200 | i = tar_block_read(t, ptr); |
| 201 | if (i != T_BLOCKSIZE) |
| 202 | { |
| 203 | if (i != -1) |
| 204 | errno = EINVAL; |
| 205 | return -1; |
| 206 | } |
| 207 | #ifdef DEBUG |
| 208 | printf(" th_read(): read block == \"%s\"\n", ptr); |
| 209 | #endif |
| 210 | } |
| 211 | #ifdef DEBUG |
| 212 | printf(" th_read(): t->th_buf.gnu_longlink == \"%s\"\n", |
| 213 | t->th_buf.gnu_longlink); |
| 214 | #endif |
| 215 | |
| 216 | i = th_read_internal(t); |
| 217 | if (i != T_BLOCKSIZE) |
| 218 | { |
| 219 | if (i != -1) |
| 220 | errno = EINVAL; |
| 221 | return -1; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* check for GNU long name extention */ |
| 226 | if (TH_ISLONGNAME(t)) |
| 227 | { |
| 228 | sz = th_get_size(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 229 | blocks = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0); |
| 230 | if (blocks > ((size_t)-1 / T_BLOCKSIZE)) |
| 231 | { |
| 232 | errno = E2BIG; |
| 233 | return -1; |
| 234 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 235 | #ifdef DEBUG |
| 236 | printf(" th_read(): GNU long filename detected " |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 237 | "(%zu bytes, %zu blocks)\n", sz, blocks); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 238 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 239 | t->th_buf.gnu_longname = (char *)malloc(blocks * T_BLOCKSIZE); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 240 | if (t->th_buf.gnu_longname == NULL) |
| 241 | return -1; |
| 242 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 243 | for (j = 0, ptr = t->th_buf.gnu_longname; j < blocks; |
| 244 | j++, ptr += T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 245 | { |
| 246 | #ifdef DEBUG |
| 247 | printf(" th_read(): reading long filename " |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 248 | "(%zu blocks left, ptr == %p)\n", blocks-j, (void *) ptr); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 249 | #endif |
| 250 | i = tar_block_read(t, ptr); |
| 251 | if (i != T_BLOCKSIZE) |
| 252 | { |
| 253 | if (i != -1) |
| 254 | errno = EINVAL; |
| 255 | return -1; |
| 256 | } |
| 257 | #ifdef DEBUG |
| 258 | printf(" th_read(): read block == \"%s\"\n", ptr); |
| 259 | #endif |
| 260 | } |
| 261 | #ifdef DEBUG |
| 262 | printf(" th_read(): t->th_buf.gnu_longname == \"%s\"\n", |
| 263 | t->th_buf.gnu_longname); |
| 264 | #endif |
| 265 | |
| 266 | i = th_read_internal(t); |
| 267 | if (i != T_BLOCKSIZE) |
| 268 | { |
| 269 | if (i != -1) |
| 270 | errno = EINVAL; |
| 271 | return -1; |
| 272 | } |
| 273 | } |
| 274 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 275 | // Extended headers (selinux contexts, posix file capabilities and encryption policies) |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 276 | while(TH_ISEXTHEADER(t) || TH_ISPOLHEADER(t)) |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 277 | { |
| 278 | sz = th_get_size(t); |
| 279 | |
| 280 | if(sz >= T_BLOCKSIZE) // Not supported |
| 281 | { |
| 282 | #ifdef DEBUG |
| 283 | printf(" th_read(): Extended header is too long!\n"); |
| 284 | #endif |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | char buf[T_BLOCKSIZE]; |
| 289 | i = tar_block_read(t, buf); |
| 290 | if (i != T_BLOCKSIZE) |
| 291 | { |
| 292 | if (i != -1) |
| 293 | errno = EINVAL; |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | // To be sure |
| 298 | buf[T_BLOCKSIZE-1] = 0; |
| 299 | |
| 300 | int len = strlen(buf); |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 301 | // posix capabilities |
| 302 | char *start = strstr(buf, CAPABILITIES_TAG); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 303 | if (start && start+CAPABILITIES_TAG_LEN < buf+len) |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 304 | { |
| 305 | start += CAPABILITIES_TAG_LEN; |
| 306 | memcpy(&t->th_buf.cap_data, start, sizeof(struct vfs_cap_data)); |
| 307 | t->th_buf.has_cap_data = 1; |
| 308 | #ifdef DEBUG |
| 309 | printf(" th_read(): Posix capabilities detected\n"); |
| 310 | #endif |
| 311 | } // end posix capabilities |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 312 | // selinux contexts |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 313 | start = strstr(buf, SELINUX_TAG); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 314 | if (start && start+SELINUX_TAG_LEN < buf+len) |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 315 | { |
| 316 | start += SELINUX_TAG_LEN; |
| 317 | char *end = strchr(start, '\n'); |
| 318 | if(end) |
| 319 | { |
| 320 | t->th_buf.selinux_context = strndup(start, end-start); |
| 321 | #ifdef DEBUG |
| 322 | printf(" th_read(): SELinux context xattr detected: %s\n", t->th_buf.selinux_context); |
| 323 | #endif |
| 324 | } |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 325 | } // end selinux contexts |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 326 | // android user.default xattr |
| 327 | start = strstr(buf, ANDROID_USER_DEFAULT_TAG); |
| 328 | if (start) |
| 329 | { |
| 330 | t->th_buf.has_user_default = 1; |
| 331 | #ifdef DEBUG |
| 332 | printf(" th_read(): android user.default xattr detected\n"); |
| 333 | #endif |
| 334 | } // end android user.default xattr |
| 335 | // android user.inode_cache xattr |
| 336 | start = strstr(buf, ANDROID_USER_CACHE_TAG); |
| 337 | if (start) |
| 338 | { |
| 339 | t->th_buf.has_user_cache = 1; |
| 340 | #ifdef DEBUG |
| 341 | printf(" th_read(): android user.inode_cache xattr detected\n"); |
| 342 | #endif |
| 343 | } // end android user.inode_cache xattr |
| 344 | // android user.inode_code_cache xattr |
| 345 | start = strstr(buf, ANDROID_USER_CODE_CACHE_TAG); |
| 346 | if (start) |
| 347 | { |
| 348 | t->th_buf.has_user_code_cache = 1; |
| 349 | #ifdef DEBUG |
| 350 | printf(" th_read(): android user.inode_code_cache xattr detected\n"); |
| 351 | #endif |
| 352 | } // end android user.inode_code_cache xattr |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 353 | |
| 354 | #ifdef USE_FSCRYPT |
| 355 | start = strstr(buf, FSCRYPT_TAG); |
| 356 | if (start && start+FSCRYPT_TAG_LEN < buf+len) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 357 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 358 | t->th_buf.fep = (struct fscrypt_policy_v1*)malloc(sizeof(struct fscrypt_policy_v1)); |
| 359 | #else |
| 360 | t->th_buf.fep = (struct fscrypt_policy_v2*)malloc(sizeof(struct fscrypt_policy_v2)); |
| 361 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 362 | if (!t->th_buf.fep) { |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 363 | printf("malloc failed for fscrypt policy\n"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | start += FSCRYPT_TAG_LEN; |
| 367 | if (*start == '0') { |
| 368 | start++; |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 369 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 370 | char *newline_check = start + sizeof(struct fscrypt_policy_v1); |
| 371 | #else |
| 372 | char *newline_check = start + sizeof(struct fscrypt_policy_v2); |
| 373 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 374 | if (*newline_check != '\n') |
| 375 | printf("did not find newline char in expected location, continuing anyway...\n"); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 376 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 377 | memcpy(t->th_buf.fep, start, sizeof(struct fscrypt_policy_v1)); |
| 378 | #else |
| 379 | memcpy(t->th_buf.fep, start, sizeof(struct fscrypt_policy_v2)); |
| 380 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 381 | #ifdef DEBUG |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 382 | printf(" th_read(): FSCrypt policy detected: %i %i %i %i %s\n", |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 383 | (int)t->th_buf.fep->version, |
| 384 | (int)t->th_buf.fep->contents_encryption_mode, |
| 385 | (int)t->th_buf.fep->filenames_encryption_mode, |
| 386 | (int)t->th_buf.fep->flags, |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 387 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 388 | t->th_buf.fep->master_key_descriptor); |
| 389 | #else |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 390 | t->th_buf.fep->master_key_identifier); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 391 | #endif |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 392 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 393 | } |
| 394 | else { |
| 395 | printf(" invalid fscrypt header found\n"); |
| 396 | } |
| 397 | } |
| 398 | #endif // USE_FSCRYPT |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | i = th_read_internal(t); |
| 402 | if (i != T_BLOCKSIZE) |
| 403 | { |
| 404 | if (i != -1) |
| 405 | errno = EINVAL; |
| 406 | return -1; |
| 407 | } |
| 408 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 409 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 413 | /* write an extended block */ |
| 414 | static int |
| 415 | th_write_extended(TAR *t, char* buf, uint64_t sz) |
| 416 | { |
| 417 | char type2; |
| 418 | uint64_t sz2; |
| 419 | int i; |
| 420 | |
| 421 | /* save old size and type */ |
| 422 | type2 = t->th_buf.typeflag; |
| 423 | sz2 = th_get_size(t); |
| 424 | |
| 425 | /* write out initial header block with fake size and type */ |
| 426 | t->th_buf.typeflag = TH_EXT_TYPE; |
| 427 | |
| 428 | if(sz >= T_BLOCKSIZE) // impossible |
| 429 | { |
| 430 | errno = EINVAL; |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | th_set_size(t, sz); |
| 435 | th_finish(t); |
| 436 | i = tar_block_write(t, &(t->th_buf)); |
| 437 | if (i != T_BLOCKSIZE) |
| 438 | { |
| 439 | if (i != -1) |
| 440 | errno = EINVAL; |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | i = tar_block_write(t, buf); |
| 445 | if (i != T_BLOCKSIZE) |
| 446 | { |
| 447 | if (i != -1) |
| 448 | errno = EINVAL; |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | /* reset type and size to original values */ |
| 453 | t->th_buf.typeflag = type2; |
| 454 | th_set_size(t, sz2); |
| 455 | memset(buf, 0, T_BLOCKSIZE); |
| 456 | return 0; |
| 457 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 458 | |
| 459 | /* write a header block */ |
| 460 | int |
| 461 | th_write(TAR *t) |
| 462 | { |
| 463 | int i, j; |
| 464 | char type2; |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 465 | uint64_t sz, sz2, total_sz = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 466 | char *ptr; |
| 467 | char buf[T_BLOCKSIZE]; |
| 468 | |
| 469 | #ifdef DEBUG |
| 470 | printf("==> th_write(TAR=\"%s\")\n", t->pathname); |
| 471 | th_print(t); |
| 472 | #endif |
| 473 | |
| 474 | if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL) |
| 475 | { |
| 476 | #ifdef DEBUG |
| 477 | printf("th_write(): using gnu_longlink (\"%s\")\n", |
| 478 | t->th_buf.gnu_longlink); |
| 479 | #endif |
| 480 | /* save old size and type */ |
| 481 | type2 = t->th_buf.typeflag; |
| 482 | sz2 = th_get_size(t); |
| 483 | |
| 484 | /* write out initial header block with fake size and type */ |
| 485 | t->th_buf.typeflag = GNU_LONGLINK_TYPE; |
| 486 | sz = strlen(t->th_buf.gnu_longlink); |
| 487 | th_set_size(t, sz); |
| 488 | th_finish(t); |
| 489 | i = tar_block_write(t, &(t->th_buf)); |
| 490 | if (i != T_BLOCKSIZE) |
| 491 | { |
| 492 | if (i != -1) |
| 493 | errno = EINVAL; |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | /* write out extra blocks containing long name */ |
| 498 | for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), |
| 499 | ptr = t->th_buf.gnu_longlink; j > 1; |
| 500 | j--, ptr += T_BLOCKSIZE) |
| 501 | { |
| 502 | i = tar_block_write(t, ptr); |
| 503 | if (i != T_BLOCKSIZE) |
| 504 | { |
| 505 | if (i != -1) |
| 506 | errno = EINVAL; |
| 507 | return -1; |
| 508 | } |
| 509 | } |
| 510 | memset(buf, 0, T_BLOCKSIZE); |
| 511 | strncpy(buf, ptr, T_BLOCKSIZE); |
| 512 | i = tar_block_write(t, &buf); |
| 513 | if (i != T_BLOCKSIZE) |
| 514 | { |
| 515 | if (i != -1) |
| 516 | errno = EINVAL; |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | /* reset type and size to original values */ |
| 521 | t->th_buf.typeflag = type2; |
| 522 | th_set_size(t, sz2); |
| 523 | } |
| 524 | |
| 525 | if ((t->options & TAR_GNU) && t->th_buf.gnu_longname != NULL) |
| 526 | { |
| 527 | #ifdef DEBUG |
| 528 | printf("th_write(): using gnu_longname (\"%s\")\n", |
| 529 | t->th_buf.gnu_longname); |
| 530 | #endif |
| 531 | /* save old size and type */ |
| 532 | type2 = t->th_buf.typeflag; |
| 533 | sz2 = th_get_size(t); |
| 534 | |
| 535 | /* write out initial header block with fake size and type */ |
| 536 | t->th_buf.typeflag = GNU_LONGNAME_TYPE; |
| 537 | sz = strlen(t->th_buf.gnu_longname); |
| 538 | th_set_size(t, sz); |
| 539 | th_finish(t); |
| 540 | i = tar_block_write(t, &(t->th_buf)); |
| 541 | if (i != T_BLOCKSIZE) |
| 542 | { |
| 543 | if (i != -1) |
| 544 | errno = EINVAL; |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | /* write out extra blocks containing long name */ |
| 549 | for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), |
| 550 | ptr = t->th_buf.gnu_longname; j > 1; |
| 551 | j--, ptr += T_BLOCKSIZE) |
| 552 | { |
| 553 | i = tar_block_write(t, ptr); |
| 554 | if (i != T_BLOCKSIZE) |
| 555 | { |
| 556 | if (i != -1) |
| 557 | errno = EINVAL; |
| 558 | return -1; |
| 559 | } |
| 560 | } |
| 561 | memset(buf, 0, T_BLOCKSIZE); |
| 562 | strncpy(buf, ptr, T_BLOCKSIZE); |
| 563 | i = tar_block_write(t, &buf); |
| 564 | if (i != T_BLOCKSIZE) |
| 565 | { |
| 566 | if (i != -1) |
| 567 | errno = EINVAL; |
| 568 | return -1; |
| 569 | } |
| 570 | |
| 571 | /* reset type and size to original values */ |
| 572 | t->th_buf.typeflag = type2; |
| 573 | th_set_size(t, sz2); |
| 574 | } |
| 575 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 576 | memset(buf, 0, T_BLOCKSIZE); |
| 577 | ptr = buf; |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 578 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 579 | if((t->options & TAR_STORE_SELINUX) && t->th_buf.selinux_context != NULL) |
| 580 | { |
| 581 | #ifdef DEBUG |
| 582 | printf("th_write(): using selinux_context (\"%s\")\n", |
| 583 | t->th_buf.selinux_context); |
| 584 | #endif |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 585 | /* setup size - EXT header has format "*size of this whole tag as ascii numbers* *space* *content* *newline* */ |
| 586 | // size newline |
| 587 | sz = SELINUX_TAG_LEN + strlen(t->th_buf.selinux_context) + 3 + 1; |
| 588 | |
| 589 | if(sz >= 100) // another ascci digit for size |
| 590 | ++sz; |
| 591 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 592 | total_sz += sz; |
| 593 | snprintf(ptr, T_BLOCKSIZE, "%d "SELINUX_TAG"%s\n", (int)sz, t->th_buf.selinux_context); |
| 594 | ptr += sz; |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 595 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 596 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 597 | #ifdef USE_FSCRYPT |
| 598 | if((t->options & TAR_STORE_FSCRYPT_POL) && t->th_buf.fep != NULL) |
| 599 | { |
| 600 | #ifdef DEBUG |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 601 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 602 | printf("th_write(): using fscrypt_policy %s\n", |
| 603 | t->th_buf.fep->master_key_descriptor); |
| 604 | #else |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 605 | printf("th_write(): using fscrypt_policy %s\n", |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 606 | t->th_buf.fep->master_key_identifier); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 607 | #endif |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 608 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 609 | /* setup size - EXT header has format "*size of this whole tag as ascii numbers* *space* *version code* *content* *newline* */ |
| 610 | // size newline |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 611 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 612 | sz = FSCRYPT_TAG_LEN + sizeof(struct fscrypt_policy_v1) + 1 + 3 + 1; |
| 613 | #else |
| 614 | sz = FSCRYPT_TAG_LEN + sizeof(struct fscrypt_policy_v2) + 1 + 3 + 1; |
| 615 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 616 | |
| 617 | if(sz >= 100) // another ascci digit for size |
| 618 | ++sz; |
| 619 | |
| 620 | if (total_sz + sz >= T_BLOCKSIZE) |
| 621 | { |
| 622 | if (th_write_extended(t, &buf[0], total_sz)) |
| 623 | return -1; |
| 624 | ptr = buf; |
| 625 | total_sz = sz; |
| 626 | } |
| 627 | else |
| 628 | total_sz += sz; |
| 629 | |
| 630 | snprintf(ptr, T_BLOCKSIZE, "%d "FSCRYPT_TAG"0", (int)sz); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 631 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 632 | memcpy(ptr + sz - sizeof(struct fscrypt_policy_v1) - 1, t->th_buf.fep, sizeof(struct fscrypt_policy_v1)); |
| 633 | #else |
| 634 | memcpy(ptr + sz - sizeof(struct fscrypt_policy_v2) - 1, t->th_buf.fep, sizeof(struct fscrypt_policy_v2)); |
| 635 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 636 | char *nlptr = ptr + sz - 1; |
| 637 | *nlptr = '\n'; |
| 638 | ptr += sz; |
| 639 | } |
| 640 | #endif |
| 641 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 642 | if((t->options & TAR_STORE_POSIX_CAP) && t->th_buf.has_cap_data) |
| 643 | { |
| 644 | #ifdef DEBUG |
| 645 | printf("th_write(): has a posix capability\n"); |
| 646 | #endif |
| 647 | sz = CAPABILITIES_TAG_LEN + sizeof(struct vfs_cap_data) + 3 + 1; |
| 648 | |
| 649 | if(sz >= 100) // another ascci digit for size |
| 650 | ++sz; |
| 651 | |
| 652 | if (total_sz + sz >= T_BLOCKSIZE) |
| 653 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 654 | if (th_write_extended(t, &buf[0], total_sz)) |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 655 | return -1; |
| 656 | ptr = buf; |
| 657 | total_sz = sz; |
| 658 | } |
| 659 | else |
| 660 | total_sz += sz; |
| 661 | |
| 662 | snprintf(ptr, T_BLOCKSIZE, "%d "CAPABILITIES_TAG, (int)sz); |
| 663 | memcpy(ptr + CAPABILITIES_TAG_LEN + 3, &t->th_buf.cap_data, sizeof(struct vfs_cap_data)); |
| 664 | char *nlptr = ptr + sz - 1; |
| 665 | *nlptr = '\n'; |
| 666 | ptr += sz; |
| 667 | } |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 668 | if (t->options & TAR_STORE_ANDROID_USER_XATTR) |
| 669 | { |
| 670 | if (t->th_buf.has_user_default) { |
| 671 | #ifdef DEBUG |
| 672 | printf("th_write(): has android user.default xattr\n"); |
| 673 | #endif |
| 674 | sz = ANDROID_USER_DEFAULT_TAG_LEN + 3 + 1; |
| 675 | |
| 676 | if (total_sz + sz >= T_BLOCKSIZE) |
| 677 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 678 | if (th_write_extended(t, &buf[0], total_sz)) |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 679 | return -1; |
| 680 | ptr = buf; |
| 681 | total_sz = sz; |
| 682 | } |
| 683 | else |
| 684 | total_sz += sz; |
| 685 | |
| 686 | snprintf(ptr, T_BLOCKSIZE, "%d "ANDROID_USER_DEFAULT_TAG, (int)sz); |
| 687 | char *nlptr = ptr + sz - 1; |
| 688 | *nlptr = '\n'; |
| 689 | ptr += sz; |
| 690 | } |
| 691 | if (t->th_buf.has_user_cache) { |
| 692 | #ifdef DEBUG |
| 693 | printf("th_write(): has android user.inode_cache xattr\n"); |
| 694 | #endif |
| 695 | sz = ANDROID_USER_CACHE_TAG_LEN + 3 + 1; |
| 696 | |
| 697 | if (total_sz + sz >= T_BLOCKSIZE) |
| 698 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 699 | if (th_write_extended(t, &buf[0], total_sz)) |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 700 | return -1; |
| 701 | ptr = buf; |
| 702 | total_sz = sz; |
| 703 | } |
| 704 | else |
| 705 | total_sz += sz; |
| 706 | |
| 707 | snprintf(ptr, T_BLOCKSIZE, "%d "ANDROID_USER_CACHE_TAG, (int)sz); |
| 708 | char *nlptr = ptr + sz - 1; |
| 709 | *nlptr = '\n'; |
| 710 | ptr += sz; |
| 711 | } |
| 712 | if (t->th_buf.has_user_code_cache) { |
| 713 | #ifdef DEBUG |
| 714 | printf("th_write(): has android user.inode_code_cache xattr\n"); |
| 715 | #endif |
| 716 | sz = ANDROID_USER_CODE_CACHE_TAG_LEN + 3 + 1; |
| 717 | |
| 718 | if (total_sz + sz >= T_BLOCKSIZE) |
| 719 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 720 | if (th_write_extended(t, &buf[0], total_sz)) |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 721 | return -1; |
| 722 | ptr = buf; |
| 723 | total_sz = sz; |
| 724 | } |
| 725 | else |
| 726 | total_sz += sz; |
| 727 | |
| 728 | snprintf(ptr, T_BLOCKSIZE, "%d "ANDROID_USER_CODE_CACHE_TAG, (int)sz); |
| 729 | char *nlptr = ptr + sz - 1; |
| 730 | *nlptr = '\n'; |
| 731 | ptr += sz; |
| 732 | } |
| 733 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 734 | if (total_sz > 0 && th_write_extended(t, &buf[0], total_sz)) // write any outstanding tar extended header |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 735 | return -1; |
| 736 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 737 | th_finish(t); |
| 738 | |
| 739 | #ifdef DEBUG |
| 740 | /* print tar header */ |
| 741 | th_print(t); |
| 742 | #endif |
| 743 | |
| 744 | i = tar_block_write(t, &(t->th_buf)); |
| 745 | if (i != T_BLOCKSIZE) |
| 746 | { |
| 747 | if (i != -1) |
| 748 | errno = EINVAL; |
| 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | #ifdef DEBUG |
| 753 | puts("th_write(): returning 0"); |
| 754 | #endif |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | |