forked from friendlyarm/android_system_vold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VolumeManager.cpp
1907 lines (1601 loc) · 53.1 KB
/
VolumeManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <linux/kdev_t.h>
#define LOG_TAG "Vold"
#include <openssl/md5.h>
#include <cutils/fs.h>
#include <cutils/log.h>
#include <selinux/android.h>
#include <sysutils/NetlinkEvent.h>
#include <private/android_filesystem_config.h>
#include "VolumeManager.h"
#include "DirectVolume.h"
#include "ResponseCode.h"
#include "Loop.h"
#include "Ext4.h"
#include "Fat.h"
#include "Devmapper.h"
#include "Process.h"
#include "Asec.h"
#include "cryptfs.h"
#define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file"
#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
+ (number & (~((1U << po2) - 1))))
/* writes superblock at end of file or device given by name */
static int writeSuperBlock(const char* name, struct asec_superblock *sb, unsigned int numImgSectors) {
int sbfd = open(name, O_RDWR);
if (sbfd < 0) {
SLOGE("Failed to open %s for superblock write (%s)", name, strerror(errno));
return -1;
}
if (lseek(sbfd, (numImgSectors * 512), SEEK_SET) < 0) {
SLOGE("Failed to lseek for superblock (%s)", strerror(errno));
close(sbfd);
return -1;
}
if (write(sbfd, sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
SLOGE("Failed to write superblock (%s)", strerror(errno));
close(sbfd);
return -1;
}
close(sbfd);
return 0;
}
static int adjustSectorNumExt4(unsigned numSectors) {
// Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
// preventing costly operations or unexpected ENOSPC error.
// Ext4::format() uses default block size without clustering.
unsigned clusterSectors = 4096 / 512;
unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
return ROUND_UP_POWER_OF_2(numSectors, 3);
}
static int adjustSectorNumFAT(unsigned numSectors) {
/*
* Add some headroom
*/
unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
numSectors += fatSize + 2;
/*
* FAT is aligned to 32 kb with 512b sectors.
*/
return ROUND_UP_POWER_OF_2(numSectors, 6);
}
static int setupLoopDevice(char* buffer, size_t len, const char* asecFileName, const char* idHash, bool debug) {
if (Loop::lookupActive(idHash, buffer, len)) {
if (Loop::create(idHash, asecFileName, buffer, len)) {
SLOGE("ASEC loop device creation failed for %s (%s)", asecFileName, strerror(errno));
return -1;
}
if (debug) {
SLOGD("New loop device created at %s", buffer);
}
} else {
if (debug) {
SLOGD("Found active loopback for %s at %s", asecFileName, buffer);
}
}
return 0;
}
static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
if (strcmp(key, "none")) {
if (Devmapper::lookupActive(idHash, buffer, len)) {
if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
buffer, len)) {
SLOGE("ASEC device mapping failed for %s (%s)", asecFileName, strerror(errno));
return -1;
}
if (debug) {
SLOGD("New devmapper instance created at %s", buffer);
}
} else {
if (debug) {
SLOGD("Found active devmapper for %s at %s", asecFileName, buffer);
}
}
*createdDMDevice = true;
} else {
strcpy(buffer, loopDevice);
*createdDMDevice = false;
}
return 0;
}
static void waitForDevMapper(const char *dmDevice) {
/*
* Wait for the device mapper node to be created. Sometimes it takes a
* while. Wait for up to 1 second. We could also inspect incoming uevents,
* but that would take more effort.
*/
int tries = 25;
while (tries--) {
if (!access(dmDevice, F_OK) || errno != ENOENT) {
break;
}
usleep(40 * 1000);
}
}
VolumeManager *VolumeManager::sInstance = NULL;
VolumeManager *VolumeManager::Instance() {
if (!sInstance)
sInstance = new VolumeManager();
return sInstance;
}
VolumeManager::VolumeManager() {
mDebug = false;
mVolumes = new VolumeCollection();
mActiveContainers = new AsecIdCollection();
mBroadcaster = NULL;
mUmsSharingCount = 0;
mSavedDirtyRatio = -1;
// set dirty ratio to 0 when UMS is active
mUmsDirtyRatio = 0;
mVolManagerDisabled = 0;
}
VolumeManager::~VolumeManager() {
delete mVolumes;
delete mActiveContainers;
}
char *VolumeManager::asecHash(const char *id, char *buffer, size_t len) {
static const char* digits = "0123456789abcdef";
unsigned char sig[MD5_DIGEST_LENGTH];
if (buffer == NULL) {
SLOGE("Destination buffer is NULL");
errno = ESPIPE;
return NULL;
} else if (id == NULL) {
SLOGE("Source buffer is NULL");
errno = ESPIPE;
return NULL;
} else if (len < MD5_ASCII_LENGTH_PLUS_NULL) {
SLOGE("Target hash buffer size < %d bytes (%zu)",
MD5_ASCII_LENGTH_PLUS_NULL, len);
errno = ESPIPE;
return NULL;
}
MD5(reinterpret_cast<const unsigned char*>(id), strlen(id), sig);
char *p = buffer;
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
*p++ = digits[sig[i] >> 4];
*p++ = digits[sig[i] & 0x0F];
}
*p = '\0';
return buffer;
}
void VolumeManager::setDebug(bool enable) {
mDebug = enable;
VolumeCollection::iterator it;
for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
(*it)->setDebug(enable);
}
}
int VolumeManager::start() {
return 0;
}
int VolumeManager::stop() {
return 0;
}
int VolumeManager::addVolume(Volume *v) {
mVolumes->push_back(v);
return 0;
}
void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
const char *devpath = evt->findParam("DEVPATH");
/* Lookup a volume to handle this device */
VolumeCollection::iterator it;
bool hit = false;
for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
if (!(*it)->handleBlockEvent(evt)) {
#ifdef NETLINK_DEBUG
SLOGD("Device '%s' event handled by volume %s\n", devpath, (*it)->getLabel());
#endif
hit = true;
break;
}
}
if (!hit) {
#ifdef NETLINK_DEBUG
SLOGW("No volumes handled block event for '%s'", devpath);
#endif
}
}
int VolumeManager::listVolumes(SocketClient *cli, bool broadcast) {
VolumeCollection::iterator i;
char msg[256];
for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
char *buffer;
asprintf(&buffer, "%s %s %d",
(*i)->getLabel(), (*i)->getFuseMountpoint(),
(*i)->getState());
cli->sendMsg(ResponseCode::VolumeListResult, buffer, false);
free(buffer);
if (broadcast) {
if((*i)->getUuid()) {
snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
(*i)->getFuseMountpoint(), (*i)->getUuid());
mBroadcaster->sendBroadcast(ResponseCode::VolumeUuidChange,
msg, false);
}
if((*i)->getUserLabel()) {
snprintf(msg, sizeof(msg), "%s %s \"%s\"", (*i)->getLabel(),
(*i)->getFuseMountpoint(), (*i)->getUserLabel());
mBroadcaster->sendBroadcast(ResponseCode::VolumeUserLabelChange,
msg, false);
}
}
}
cli->sendMsg(ResponseCode::CommandOkay, "Volumes listed.", false);
return 0;
}
int VolumeManager::formatVolume(const char *label, bool wipe) {
Volume *v = lookupVolume(label);
if (!v) {
errno = ENOENT;
return -1;
}
if (mVolManagerDisabled) {
errno = EBUSY;
return -1;
}
return v->formatVol(wipe);
}
int VolumeManager::getObbMountPath(const char *sourceFile, char *mountPath, int mountPathLen) {
char idHash[33];
if (!asecHash(sourceFile, idHash, sizeof(idHash))) {
SLOGE("Hash of '%s' failed (%s)", sourceFile, strerror(errno));
return -1;
}
memset(mountPath, 0, mountPathLen);
int written = snprintf(mountPath, mountPathLen, "%s/%s", Volume::LOOPDIR, idHash);
if ((written < 0) || (written >= mountPathLen)) {
errno = EINVAL;
return -1;
}
if (access(mountPath, F_OK)) {
errno = ENOENT;
return -1;
}
return 0;
}
int VolumeManager::getAsecMountPath(const char *id, char *buffer, int maxlen) {
char asecFileName[255];
if (!isLegalAsecId(id)) {
SLOGE("getAsecMountPath: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);
return -1;
}
memset(buffer, 0, maxlen);
if (access(asecFileName, F_OK)) {
errno = ENOENT;
return -1;
}
int written = snprintf(buffer, maxlen, "%s/%s", Volume::ASECDIR, id);
if ((written < 0) || (written >= maxlen)) {
SLOGE("getAsecMountPath failed for %s: couldn't construct path in buffer", id);
errno = EINVAL;
return -1;
}
return 0;
}
int VolumeManager::getAsecFilesystemPath(const char *id, char *buffer, int maxlen) {
char asecFileName[255];
if (!isLegalAsecId(id)) {
SLOGE("getAsecFilesystemPath: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);
return -1;
}
memset(buffer, 0, maxlen);
if (access(asecFileName, F_OK)) {
errno = ENOENT;
return -1;
}
int written = snprintf(buffer, maxlen, "%s", asecFileName);
if ((written < 0) || (written >= maxlen)) {
errno = EINVAL;
return -1;
}
return 0;
}
int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
const char *key, const int ownerUid, bool isExternal) {
struct asec_superblock sb;
memset(&sb, 0, sizeof(sb));
if (!isLegalAsecId(id)) {
SLOGE("createAsec: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
const bool wantFilesystem = strcmp(fstype, "none");
bool usingExt4 = false;
if (wantFilesystem) {
usingExt4 = !strcmp(fstype, "ext4");
if (usingExt4) {
sb.c_opts |= ASEC_SB_C_OPTS_EXT4;
} else if (strcmp(fstype, "fat")) {
SLOGE("Invalid filesystem type %s", fstype);
errno = EINVAL;
return -1;
}
}
sb.magic = ASEC_SB_MAGIC;
sb.ver = ASEC_SB_VER;
if (numSectors < ((1024*1024)/512)) {
SLOGE("Invalid container size specified (%d sectors)", numSectors);
errno = EINVAL;
return -1;
}
if (lookupVolume(id)) {
SLOGE("ASEC id '%s' currently exists", id);
errno = EADDRINUSE;
return -1;
}
char asecFileName[255];
if (!findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
asecFileName, strerror(errno));
errno = EADDRINUSE;
return -1;
}
const char *asecDir = isExternal ? Volume::SEC_ASECDIR_EXT : Volume::SEC_ASECDIR_INT;
int written = snprintf(asecFileName, sizeof(asecFileName), "%s/%s.asec", asecDir, id);
if ((written < 0) || (size_t(written) >= sizeof(asecFileName))) {
errno = EINVAL;
return -1;
}
if (!access(asecFileName, F_OK)) {
SLOGE("ASEC file '%s' currently exists - destroy it first! (%s)",
asecFileName, strerror(errno));
errno = EADDRINUSE;
return -1;
}
unsigned numImgSectors;
if (usingExt4)
numImgSectors = adjustSectorNumExt4(numSectors);
else
numImgSectors = adjustSectorNumFAT(numSectors);
// Add +1 for our superblock which is at the end
if (Loop::createImageFile(asecFileName, numImgSectors + 1)) {
SLOGE("ASEC image file creation failed (%s)", strerror(errno));
return -1;
}
char idHash[33];
if (!asecHash(id, idHash, sizeof(idHash))) {
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
unlink(asecFileName);
return -1;
}
char loopDevice[255];
if (Loop::create(idHash, asecFileName, loopDevice, sizeof(loopDevice))) {
SLOGE("ASEC loop device creation failed (%s)", strerror(errno));
unlink(asecFileName);
return -1;
}
char dmDevice[255];
bool cleanupDm = false;
if (strcmp(key, "none")) {
// XXX: This is all we support for now
sb.c_cipher = ASEC_SB_C_CIPHER_TWOFISH;
if (Devmapper::create(idHash, loopDevice, key, numImgSectors, dmDevice,
sizeof(dmDevice))) {
SLOGE("ASEC device mapping failed (%s)", strerror(errno));
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
cleanupDm = true;
} else {
sb.c_cipher = ASEC_SB_C_CIPHER_NONE;
strcpy(dmDevice, loopDevice);
}
/*
* Drop down the superblock at the end of the file
*/
if (writeSuperBlock(loopDevice, &sb, numImgSectors)) {
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
if (wantFilesystem) {
int formatStatus;
char mountPoint[255];
int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("ASEC fs format failed: couldn't construct mountPoint");
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
if (usingExt4) {
formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint);
} else {
formatStatus = Fat::format(dmDevice, numImgSectors, 0);
}
if (formatStatus < 0) {
SLOGE("ASEC fs format failed (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
if (mkdir(mountPoint, 0000)) {
if (errno != EEXIST) {
SLOGE("Mountpoint creation failed (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
}
int mountStatus;
if (usingExt4) {
mountStatus = Ext4::doMount(dmDevice, mountPoint, false, false, false);
} else {
mountStatus = Fat::doMount(dmDevice, mountPoint, false, false, false, ownerUid, 0, 0000,
false);
}
if (mountStatus) {
SLOGE("ASEC FAT mount failed (%s)", strerror(errno));
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
unlink(asecFileName);
return -1;
}
if (usingExt4) {
int dirfd = open(mountPoint, O_DIRECTORY);
if (dirfd >= 0) {
if (fchown(dirfd, ownerUid, AID_SYSTEM)
|| fchmod(dirfd, S_IRUSR | S_IWUSR | S_IXUSR | S_ISGID | S_IRGRP | S_IXGRP)) {
SLOGI("Cannot chown/chmod new ASEC mount point %s", mountPoint);
}
close(dirfd);
}
}
} else {
SLOGI("Created raw secure container %s (no filesystem)", id);
}
mActiveContainers->push_back(new ContainerData(strdup(id), ASEC));
return 0;
}
int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
char asecFileName[255];
char mountPoint[255];
bool cleanupDm = false;
if (!isLegalAsecId(id)) {
SLOGE("resizeAsec: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);
return -1;
}
int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("ASEC resize failed for %s: couldn't construct mountpoint", id);
return -1;
}
if (isMountpointMounted(mountPoint)) {
SLOGE("ASEC %s mounted. Unmount before resizing", id);
errno = EBUSY;
return -1;
}
struct asec_superblock sb;
int fd;
unsigned int oldNumSec = 0;
if ((fd = open(asecFileName, O_RDONLY)) < 0) {
SLOGE("Failed to open ASEC file (%s)", strerror(errno));
return -1;
}
struct stat info;
if (fstat(fd, &info) < 0) {
SLOGE("Failed to get file size (%s)", strerror(errno));
close(fd);
return -1;
}
oldNumSec = info.st_size / 512;
unsigned numImgSectors;
if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
numImgSectors = adjustSectorNumExt4(numSectors);
else
numImgSectors = adjustSectorNumFAT(numSectors);
/*
* add one block for the superblock
*/
SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
if (oldNumSec == numImgSectors + 1) {
SLOGW("Size unchanged; ignoring resize request");
return 0;
} else if (oldNumSec > numImgSectors + 1) {
SLOGE("Only growing is currently supported.");
close(fd);
return -1;
}
/*
* Try to read superblock.
*/
memset(&sb, 0, sizeof(struct asec_superblock));
if (lseek(fd, ((oldNumSec - 1) * 512), SEEK_SET) < 0) {
SLOGE("lseek failed (%s)", strerror(errno));
close(fd);
return -1;
}
if (read(fd, &sb, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
SLOGE("superblock read failed (%s)", strerror(errno));
close(fd);
return -1;
}
close(fd);
if (mDebug) {
SLOGD("Container sb magic/ver (%.8x/%.2x)", sb.magic, sb.ver);
}
if (sb.magic != ASEC_SB_MAGIC || sb.ver != ASEC_SB_VER) {
SLOGE("Bad container magic/version (%.8x/%.2x)", sb.magic, sb.ver);
errno = EMEDIUMTYPE;
return -1;
}
if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
SLOGE("Only ext4 partitions are supported for resize");
errno = EINVAL;
return -1;
}
if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
SLOGE("Resize of ASEC image file failed. Could not resize %s", id);
return -1;
}
/*
* Drop down a copy of the superblock at the end of the file
*/
if (writeSuperBlock(asecFileName, &sb, numImgSectors))
goto fail;
char idHash[33];
if (!asecHash(id, idHash, sizeof(idHash))) {
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
goto fail;
}
char loopDevice[255];
if (setupLoopDevice(loopDevice, sizeof(loopDevice), asecFileName, idHash, mDebug))
goto fail;
char dmDevice[255];
if (setupDevMapperDevice(dmDevice, sizeof(dmDevice), loopDevice, asecFileName, key, idHash, numImgSectors, &cleanupDm, mDebug)) {
Loop::destroyByDevice(loopDevice);
goto fail;
}
/*
* Wait for the device mapper node to be created.
*/
waitForDevMapper(dmDevice);
if (Ext4::resize(dmDevice, numImgSectors)) {
SLOGE("Unable to resize %s (%s)", id, strerror(errno));
if (cleanupDm) {
Devmapper::destroy(idHash);
}
Loop::destroyByDevice(loopDevice);
goto fail;
}
return 0;
fail:
Loop::resizeImageFile(asecFileName, oldNumSec);
return -1;
}
int VolumeManager::finalizeAsec(const char *id) {
char asecFileName[255];
char loopDevice[255];
char mountPoint[255];
if (!isLegalAsecId(id)) {
SLOGE("finalizeAsec: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);
return -1;
}
char idHash[33];
if (!asecHash(id, idHash, sizeof(idHash))) {
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
return -1;
}
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
SLOGE("Unable to finalize %s (%s)", id, strerror(errno));
return -1;
}
unsigned int nr_sec = 0;
struct asec_superblock sb;
if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
return -1;
}
int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("ASEC finalize failed: couldn't construct mountPoint");
return -1;
}
int result = 0;
if (sb.c_opts & ASEC_SB_C_OPTS_EXT4) {
result = Ext4::doMount(loopDevice, mountPoint, true, true, true);
} else {
result = Fat::doMount(loopDevice, mountPoint, true, true, true, 0, 0, 0227, false);
}
if (result) {
SLOGE("ASEC finalize mount failed (%s)", strerror(errno));
return -1;
}
if (mDebug) {
SLOGD("ASEC %s finalized", id);
}
return 0;
}
int VolumeManager::fixupAsecPermissions(const char *id, gid_t gid, const char* filename) {
char asecFileName[255];
char loopDevice[255];
char mountPoint[255];
if (gid < AID_APP) {
SLOGE("Group ID is not in application range");
return -1;
}
if (!isLegalAsecId(id)) {
SLOGE("fixupAsecPermissions: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);
return -1;
}
char idHash[33];
if (!asecHash(id, idHash, sizeof(idHash))) {
SLOGE("Hash of '%s' failed (%s)", id, strerror(errno));
return -1;
}
if (Loop::lookupActive(idHash, loopDevice, sizeof(loopDevice))) {
SLOGE("Unable fix permissions during lookup on %s (%s)", id, strerror(errno));
return -1;
}
unsigned int nr_sec = 0;
struct asec_superblock sb;
if (Loop::lookupInfo(loopDevice, &sb, &nr_sec)) {
return -1;
}
int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("Unable remount to fix permissions for %s: couldn't construct mountpoint", id);
return -1;
}
int result = 0;
if ((sb.c_opts & ASEC_SB_C_OPTS_EXT4) == 0) {
return 0;
}
int ret = Ext4::doMount(loopDevice, mountPoint,
false /* read-only */,
true /* remount */,
false /* executable */);
if (ret) {
SLOGE("Unable remount to fix permissions for %s (%s)", id, strerror(errno));
return -1;
}
char *paths[] = { mountPoint, NULL };
FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL);
if (fts) {
// Traverse the entire hierarchy and chown to system UID.
for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
// We don't care about the lost+found directory.
if (!strcmp(ftsent->fts_name, "lost+found")) {
continue;
}
/*
* There can only be one file marked as private right now.
* This should be more robust, but it satisfies the requirements
* we have for right now.
*/
const bool privateFile = !strcmp(ftsent->fts_name, filename);
int fd = open(ftsent->fts_accpath, O_NOFOLLOW);
if (fd < 0) {
SLOGE("Couldn't open file %s: %s", ftsent->fts_accpath, strerror(errno));
result = -1;
continue;
}
result |= fchown(fd, AID_SYSTEM, privateFile? gid : AID_SYSTEM);
if (ftsent->fts_info & FTS_D) {
result |= fchmod(fd, 0755);
} else if (ftsent->fts_info & FTS_F) {
result |= fchmod(fd, privateFile ? 0640 : 0644);
}
if (selinux_android_restorecon(ftsent->fts_path, 0) < 0) {
SLOGE("restorecon failed for %s: %s\n", ftsent->fts_path, strerror(errno));
result |= -1;
}
close(fd);
}
fts_close(fts);
// Finally make the directory readable by everyone.
int dirfd = open(mountPoint, O_DIRECTORY);
if (dirfd < 0 || fchmod(dirfd, 0755)) {
SLOGE("Couldn't change owner of existing directory %s: %s", mountPoint, strerror(errno));
result |= -1;
}
close(dirfd);
} else {
result |= -1;
}
result |= Ext4::doMount(loopDevice, mountPoint,
true /* read-only */,
true /* remount */,
true /* execute */);
if (result) {
SLOGE("ASEC fix permissions failed (%s)", strerror(errno));
return -1;
}
if (mDebug) {
SLOGD("ASEC %s permissions fixed", id);
}
return 0;
}
int VolumeManager::renameAsec(const char *id1, const char *id2) {
char asecFilename1[255];
char *asecFilename2;
char mountPoint[255];
const char *dir;
if (!isLegalAsecId(id1)) {
SLOGE("renameAsec: Invalid asec id1 \"%s\"", id1);
errno = EINVAL;
return -1;
}
if (!isLegalAsecId(id2)) {
SLOGE("renameAsec: Invalid asec id2 \"%s\"", id2);
errno = EINVAL;
return -1;
}
if (findAsec(id1, asecFilename1, sizeof(asecFilename1), &dir)) {
SLOGE("Couldn't find ASEC %s", id1);
return -1;
}
asprintf(&asecFilename2, "%s/%s.asec", dir, id2);
int written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id1);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("Rename failed: couldn't construct mountpoint");
goto out_err;
}
if (isMountpointMounted(mountPoint)) {
SLOGW("Rename attempt when src mounted");
errno = EBUSY;
goto out_err;
}
written = snprintf(mountPoint, sizeof(mountPoint), "%s/%s", Volume::ASECDIR, id2);
if ((written < 0) || (size_t(written) >= sizeof(mountPoint))) {
SLOGE("Rename failed: couldn't construct mountpoint2");
goto out_err;
}
if (isMountpointMounted(mountPoint)) {
SLOGW("Rename attempt when dst mounted");
errno = EBUSY;
goto out_err;
}
if (!access(asecFilename2, F_OK)) {
SLOGE("Rename attempt when dst exists");
errno = EADDRINUSE;
goto out_err;
}
if (rename(asecFilename1, asecFilename2)) {
SLOGE("Rename of '%s' to '%s' failed (%s)", asecFilename1, asecFilename2, strerror(errno));
goto out_err;
}
free(asecFilename2);
return 0;
out_err:
free(asecFilename2);
return -1;
}
#define UNMOUNT_RETRIES 5
#define UNMOUNT_SLEEP_BETWEEN_RETRY_MS (1000 * 1000)
int VolumeManager::unmountAsec(const char *id, bool force) {
char asecFileName[255];
char mountPoint[255];
if (!isLegalAsecId(id)) {
SLOGE("unmountAsec: Invalid asec id \"%s\"", id);
errno = EINVAL;
return -1;
}
if (findAsec(id, asecFileName, sizeof(asecFileName))) {
SLOGE("Couldn't find ASEC %s", id);