Whamcloud - gitweb
e2fsck: check for consistent encryption policies
[tools/e2fsprogs.git] / tests / f_bad_encryption / mkimage.sh
1 #!/bin/bash
2 #
3 # This is the script that was used to create the image.gz in this directory.
4 #
5 # This requires a patched version of debugfs that understands the "fscrypt."
6 # xattr name prefix, so that the encryption xattrs can be manipulated.
7
8 set -e -u
9 umask 0022
10
11 do_debugfs() {
12         umount mnt
13         debugfs -w "$@" image
14         mount image mnt
15 }
16
17 create_encrypted_file() {
18         local file=$1
19         local ino
20
21         echo foo > "$file"
22
23         # not needed, but makes image more compressible
24         ino=$(stat -c %i "$file")
25         do_debugfs -R "zap_block -f <$ino> 0"
26 }
27
28 set_encryption_xattr() {
29         local file=$1
30         local value=$2
31         local ino
32
33         ino=$(stat -c %i "$file")
34         do_debugfs -R "ea_set <$ino> fscrypt.c $value"
35 }
36
37 rm_encryption_xattr() {
38         local file=$1
39         local ino
40
41         ino=$(stat -c %i "$file")
42         do_debugfs -R "ea_rm <$ino> fscrypt.c"
43 }
44
45 clear_encrypt_flag() {
46         local file=$1
47         local ino
48
49         ino=$(stat -c %i "$file")
50         do_debugfs -R "set_inode_field <$ino> flags 0"
51 }
52
53 clear_encryption() {
54         local file=$1
55         local ino
56         local is_symlink=false
57
58         if [ -L "$file" ]; then
59                 is_symlink=true
60         fi
61         ino=$(stat -c %i "$file")
62
63         do_debugfs -R "ea_rm <$ino> fscrypt.c"
64         do_debugfs -R "set_inode_field <$ino> flags 0"
65         if $is_symlink; then
66                 do_debugfs -R "set_inode_field <$ino> block[0] 0xAAAAAAAA"
67                 do_debugfs -R "set_inode_field <$ino> block[1] 0"
68                 do_debugfs -R "set_inode_field <$ino> size 4"
69         fi
70 }
71
72 mkdir -p mnt
73 umount mnt &> /dev/null || true
74
75 dd if=/dev/zero of=image bs=4096 count=128
76 mke2fs -O encrypt -b 4096 -N 128 image
77 mount image mnt
78
79 # Create an encrypted directory (ino 12)
80 dir=mnt/edir
81 mkdir $dir
82 echo password | e4crypt add_key $dir
83
84 # Control cases: valid encrypted regular file, dir, and symlink (ino 13-15)
85 create_encrypted_file $dir/encrypted_file
86 mkdir $dir/encrypted_dir
87 ln -s target $dir/encrypted_symlink
88
89 # Control case: file type that is never encrypted (ino 16)
90 mkfifo $dir/fifo
91
92 # Inodes with missing encryption xattr (ino 17-18).
93 # e2fsck should offer to clear the encrypt flag on these inodes.
94
95 create_encrypted_file $dir/missing_xattr_file
96 rm_encryption_xattr $dir/missing_xattr_file
97
98 mkdir $dir/missing_xattr_dir
99 rm_encryption_xattr $dir/missing_xattr_dir
100
101 # Inodes with corrupt encryption xattr (ino 19-22).
102 # e2fsck should offer to clear these inodes.
103
104 create_encrypted_file $dir/corrupt_xattr_1
105 set_encryption_xattr $dir/corrupt_xattr_1 '\0'
106
107 create_encrypted_file $dir/corrupt_xattr_2
108 set_encryption_xattr $dir/corrupt_xattr_2 \
109         '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'
110
111 create_encrypted_file $dir/corrupt_xattr_3
112 set_encryption_xattr $dir/corrupt_xattr_3 '\1'
113
114 create_encrypted_file $dir/corrupt_xattr_4
115 set_encryption_xattr $dir/corrupt_xattr_4 '\2'
116
117 # Unencrypted inodes in encrypted directory (ino 23-25).
118 # e2fsck should offer to clear these directory entries.
119
120 create_encrypted_file $dir/unencrypted_file
121 clear_encryption $dir/unencrypted_file
122
123 mkdir $dir/unencrypted_dir
124 clear_encryption $dir/unencrypted_dir
125
126 ln -s target $dir/unencrypted_symlink
127 clear_encryption $dir/unencrypted_symlink
128
129 # Inodes with different encryption policy in encrypted directory (ino 26-29).
130 # e2fsck should offer to clear these directory entries.
131
132 xattr='\1\1\4\0AAAAAAAABBBBBBBBBBBBBBBB'
133
134 create_encrypted_file $dir/inconsistent_file_1
135 set_encryption_xattr $dir/inconsistent_file_1 $xattr
136
137 mkdir $dir/inconsistent_dir
138 set_encryption_xattr $dir/inconsistent_dir $xattr
139
140 ln -s target $dir/inconsistent_symlink
141 set_encryption_xattr $dir/inconsistent_symlink $xattr
142
143 xattr='\2\1\4\0\0\0\0\0AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB'
144 create_encrypted_file $dir/inconsistent_file_2
145 set_encryption_xattr $dir/inconsistent_file_2 $xattr
146
147 # Encrypted file and directory with valid v2 encryption policy (ino 30-31).
148 # e2fsck shouldn't change these.
149 dir2=mnt/edir2
150 mkdir $dir2
151 echo password | e4crypt add_key $dir2
152 xattr='\2\1\4\0\0\0\0\0AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB'
153 create_encrypted_file $dir2/file
154 set_encryption_xattr $dir2/file $xattr
155 set_encryption_xattr $dir2 $xattr
156
157 # Encrypted file and directory with unrecognized encryption policy version
158 # (ino 32-33).  e2fsck shouldn't change these.
159 dir3=mnt/edir3
160 mkdir $dir3
161 echo password | e4crypt add_key $dir3
162 xattr='\3'
163 create_encrypted_file $dir3/file
164 set_encryption_xattr $dir3/file $xattr
165 set_encryption_xattr $dir3 $xattr
166
167 umount mnt
168 rmdir mnt
169 gzip -9 -f image