blob: aa7f88e84d5fe54478cb01d598ac27789cb97e28 [file] [log] [blame]
Dees Troy89310072013-11-11 15:14:14 +00001#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <sys/ioctl.h>
9#include <linux/dm-ioctl.h>
10#include <sys/mount.h>
11#include "../fs_mgr/include/fs_mgr.h"
12#include "cryptfs.h"
13
14#include "cutils/properties.h"
15
16#ifndef PROPERTY_VALUE_MAX
17#define PROPERTY_VALUE_MAX 255
18#endif
19#ifndef FSTAB_PREFIX
20#define FSTAB_PREFIX "/fstab."
21#endif
22#ifndef KEY_IN_FOOTER
23#define KEY_IN_FOOTER "footer"
24#endif
25
26struct fstab *fstab;
27
28static unsigned int get_blkdev_size(int fd)
29{
30 unsigned int nr_sec;
31
32 if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
33 nr_sec = 0;
34 }
35
36 return nr_sec;
37}
38
39int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
40{
41 static int cached_data = 0;
42 static off64_t cached_off = 0;
43 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
44 int fd;
45 char key_loc[PROPERTY_VALUE_MAX];
46 char real_blkdev[PROPERTY_VALUE_MAX];
47 unsigned int nr_sec;
48 int rc = -1;
49
50 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
51
52 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
53 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
54 printf("Cannot open real block device %s\n", real_blkdev);
55 return -1;
56 }
57
58 if ((nr_sec = get_blkdev_size(fd))) {
59 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
60 * encryption info footer and key, and plenty of bytes to spare for future
61 * growth.
62 */
63 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
64 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
65 cached_data = 1;
66 } else {
67 printf("Cannot get size of block device %s\n", real_blkdev);
68 }
69 close(fd);
70 } else {
71 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
72 cached_off = 0;
73 cached_data = 1;
74 }
75
76 if (cached_data) {
77 if (metadata_fname) {
78 *metadata_fname = cached_metadata_fname;
79 }
80 if (off) {
81 *off = cached_off;
82 }
83 rc = 0;
84 }
85
86 return rc;
87}
88
89int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
90{
91 int fd;
92 unsigned int nr_sec, cnt;
93 off64_t starting_off;
94 int rc = -1;
95 char *fname = NULL;
96 struct stat statbuf;
97
98 if (get_crypt_ftr_info(&fname, &starting_off)) {
99 printf("Unable to get crypt_ftr_info\n");
100 return -1;
101 }
102 if (fname[0] != '/') {
103 printf("Unexpected value for crypto key location '%s'\n", fname);
104 //return -1;
105 }
106 if ( (fd = open(fname, O_RDWR)) < 0) {
107 printf("Cannot open footer file %s for get\n", fname);
108 return -1;
109 }
110
111 /* Make sure it's 16 Kbytes in length */
112 fstat(fd, &statbuf);
113 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
114 printf("footer file %s is not the expected size!\n", fname);
115 close(fd);
116 return -1;
117 }
118
119 /* Seek to the start of the crypt footer */
120 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
121 printf("Cannot seek to real block device footer\n");
122 close(fd);
123 return -1;
124 }
125
126 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
127 printf("Cannot read real block device footer\n");
128 close(fd);
129 return -1;
130 }
131 close(fd);
132 return 0;
133}
134
135int main(void)
136{
137 char key_loc[PROPERTY_VALUE_MAX];
138 char blk_dev[PROPERTY_VALUE_MAX];
139 char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
140 struct stat st;
141 struct crypt_mnt_ftr crypt_ftr;
142 int fdout;
143
144 printf("This tool comes with no warranties whatsoever.\n");
145 printf("http://teamw.in\n\n");
146 strcpy(fstab_filename, FSTAB_PREFIX);
147 property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, "");
148
149 if (stat(fstab_filename, &st) != 0) {
150 printf("Cannot locate fstab file '%s'\n", fstab_filename);
151 return -1;
152 }
153
154 fstab = fs_mgr_read_fstab(fstab_filename);
155 if (!fstab) {
156 printf("failed to open %s\n", fstab_filename);
157 return -1;
158 }
159
160 fs_mgr_get_crypt_info(fstab, key_loc, blk_dev, sizeof(blk_dev));
161
162 if (get_crypt_ftr_and_key(&crypt_ftr)) {
163 printf("Error getting crypt footer and key\n");
164 return -1;
165 }
166
167 if ( (fdout = open("/footerfile", O_WRONLY | O_CREAT, 0644)) < 0) {
168 printf("Cannot open output file /footerfile\n");
169 return -1;
170 }
171 if (write(fdout, (void*) &crypt_ftr, sizeof(struct crypt_mnt_ftr)) != sizeof(struct crypt_mnt_ftr)) {
172 printf("Failed to write footer.\n");
173 }
174 close(fdout);
175
176 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
177 unsigned int nr_sec, cnt;
178 off64_t off = 0;
179 char buffer[CRYPT_FOOTER_OFFSET];
180 int fd;
181
182 printf("\n\nDumping footer from '%s'...\n", blk_dev);
183 if ( (fd = open(blk_dev, O_RDONLY)) < 0) {
184 printf("Cannot open real block device %s\n", blk_dev);
185 return -1;
186 }
187
188 if ((nr_sec = get_blkdev_size(fd))) {
189 off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
190 } else {
191 printf("Cannot get size of block device %s\n", blk_dev);
192 close(fd);
193 return -1;
194 }
195 printf("Size is %llu, offset is %llu\n", ((off64_t)nr_sec * 512), off);
196 if (lseek64(fd, off, SEEK_SET) == -1) {
197 printf("Cannot seek to real block device footer\n");
198 close(fd);
199 return -1;
200 }
201
202 if ( (cnt = read(fd, buffer, sizeof(buffer))) != sizeof(buffer)) {
203 printf("Cannot read real block device footer\n");
204 close(fd);
205 return -1;
206 }
207 close(fd);
208 if ( (fdout = open("/footerdump", O_WRONLY | O_CREAT, 0644)) < 0) {
209 printf("Cannot open output file /footerdump\n");
210 return -1;
211 }
212 if (write(fdout, buffer, sizeof(buffer)) != sizeof(buffer)) {
213 printf("Failed to write footer.\n");
214 }
215 close(fdout);
216 }
217
218 return 0;
219}