]> git.saurik.com Git - apple/security.git/blob - sec/securityd/Regressions/secd-03-corrupted-items.c
Security-55471.14.8.tar.gz
[apple/security.git] / sec / securityd / Regressions / secd-03-corrupted-items.c
1 //
2 // secd-03-corrupted-item.c
3 // sec
4 //
5 // Created by Fabrice Gautier on 06/19/13.
6 //
7 //
8
9 #include "secd_regressions.h"
10
11 #include <securityd/SecDbItem.h>
12 #include <utilities/array_size.h>
13 #include <utilities/SecCFWrappers.h>
14 #include <utilities/SecFileLocations.h>
15 #include <utilities/fileIo.h>
16
17 #include <securityd/SOSCloudCircleServer.h>
18 #include <securityd/SecItemServer.h>
19
20 #include <Security/SecBasePriv.h>
21
22 #include <AssertMacros.h>
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <pthread.h>
28
29 #include "SecdTestKeychainUtilities.h"
30
31 static OSStatus do_query(void)
32 {
33 /* querying a password */
34 const void *keys[] = {
35 kSecClass,
36 kSecAttrServer,
37 kSecReturnAttributes,
38 };
39 const void *values[] = {
40 kSecClassInternetPassword,
41 CFSTR("corrupt.spamcop.net"),
42 kCFBooleanTrue,
43 };
44 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
45 array_size(keys), NULL, NULL);
46 CFTypeRef results = NULL;
47
48 OSStatus err = SecItemCopyMatching(query, &results);
49 CFReleaseNull(query);
50 return err;
51 }
52
53 static void *do_add(void *arg)
54 {
55 int tid=(int)(arg);
56
57 for(int i=0;i<20;i++) {
58 /* Creating a password */
59 SInt32 v_eighty = (tid+1)*1000+i;
60 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
61 const char *v_data = "test";
62 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
63 const void *keys[] = {
64 kSecClass,
65 kSecAttrServer,
66 kSecAttrAccount,
67 kSecAttrPort,
68 kSecAttrProtocol,
69 kSecAttrAuthenticationType,
70 kSecValueData
71 };
72 const void *values[] = {
73 kSecClassInternetPassword,
74 CFSTR("members.spamcop.net"),
75 CFSTR("smith"),
76 eighty,
77 CFSTR("http"),
78 CFSTR("dflt"),
79 pwdata
80 };
81
82 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
83 array_size(keys), NULL, NULL);
84
85 ok_status(SecItemAdd(item, NULL), "add internet password");
86 }
87
88 return NULL;
89 }
90
91
92 #define N_THREADS 10
93
94 static const char *corrupt_item_sql = "UPDATE inet SET data=X'12345678' WHERE rowid=1";
95
96
97 int secd_03_corrupted_items(int argc, char *const *argv)
98 {
99 plan_tests(4 + N_THREADS*21 + kSecdTestSetupTestCount);
100
101 /* custom keychain dir */
102 secd_test_setup_temp_keychain("secd_03_corrupted_items", NULL);
103
104 /* add a password */
105 int v_eighty = 80;
106 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
107 const char *v_data = "test";
108 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
109 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
110 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
111 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("corrupt.spamcop.net"));
112 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
113 CFDictionaryAddValue(query, kSecAttrPort, eighty);
114 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
115 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
116 CFDictionaryAddValue(query, kSecValueData, pwdata);
117 ok_status(SecItemAdd(query, NULL), "add internet password");
118
119 /* corrupt the password */
120 CFStringRef keychain_path_cf = __SecKeychainCopyPath();
121
122 CFStringPerformWithCString(keychain_path_cf, ^(const char *keychain_path) {
123 /* Create a new keychain sqlite db */
124 sqlite3 *db;
125
126 is(sqlite3_open(keychain_path, &db), SQLITE_OK, "create keychain");
127 is(sqlite3_exec(db, corrupt_item_sql, NULL, NULL, NULL), SQLITE_OK,
128 "corrupting keychain item1");
129
130 });
131
132 pthread_t add_thread[N_THREADS];
133 void *add_err[N_THREADS] = {NULL,};
134
135 for(int i=0; i<N_THREADS; i++)
136 pthread_create(&add_thread[i], NULL, do_add, (void*)(intptr_t)i);
137
138 is_status(do_query(), errSecItemNotFound, "query");
139
140 for(int i=0; i<N_THREADS; i++)
141 pthread_join(add_thread[i], &add_err[i]);
142
143 for(int i=0; i<N_THREADS; i++)
144 ok(add_err[i]==NULL, "add thread");
145
146 return 0;
147 }