]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-05-corrupted-items.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-05-corrupted-items.c
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include "secd_regressions.h"
26
27 #include <securityd/SecDbItem.h>
28 #include <utilities/array_size.h>
29 #include <utilities/SecCFWrappers.h>
30 #include <utilities/SecFileLocations.h>
31 #include <utilities/fileIo.h>
32
33 #include <securityd/SOSCloudCircleServer.h>
34 #include <securityd/SecItemServer.h>
35
36 #include <Security/SecBasePriv.h>
37
38 #include <AssertMacros.h>
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <sys/stat.h>
43 #include <pthread.h>
44
45 #include "SecdTestKeychainUtilities.h"
46
47 #define N_ITEMS (100)
48 #define N_THREADS (10)
49 #define N_ADDS (20)
50
51 static void *do_add(void *arg)
52 {
53 int tid=(int)(arg);
54
55 for(int i=0;i<N_ADDS;i++) {
56 /* Creating a password */
57 SInt32 v_eighty = (tid+1)*1000+i;
58 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
59 const char *v_data = "test";
60 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
61 const void *keys[] = {
62 kSecClass,
63 kSecAttrServer,
64 kSecAttrAccount,
65 kSecAttrPort,
66 kSecAttrProtocol,
67 kSecAttrAuthenticationType,
68 kSecValueData
69 };
70 const void *values[] = {
71 kSecClassInternetPassword,
72 CFSTR("members.spamcop.net"),
73 CFSTR("smith"),
74 eighty,
75 CFSTR("http"),
76 CFSTR("dflt"),
77 pwdata
78 };
79
80 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
81 array_size(keys), NULL, NULL);
82
83 ok_status(SecItemAdd(item, NULL), "add internet password");
84 CFReleaseNull(eighty);
85 CFReleaseNull(pwdata);
86 CFReleaseNull(item);
87 }
88
89 return NULL;
90 }
91
92
93 int secd_05_corrupted_items(int argc, char *const *argv)
94 {
95 plan_tests(1 + N_THREADS*(N_ADDS+1) + N_ITEMS*4 + kSecdTestSetupTestCount);
96
97 /* custom keychain dir */
98 secd_test_setup_temp_keychain("secd_05_corrupted_items", NULL);
99
100 /* add a password */
101 const char *v_data = "test";
102 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
103 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
104 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
105 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("corrupt.spamcop.net"));
106 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
107 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
108 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
109 CFDictionaryAddValue(query, kSecValueData, pwdata);
110
111 SInt32 i;
112 for(i=1; i<=N_ITEMS; i++) {
113 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
114 CFDictionarySetValue(query, kSecAttrPort, port);
115 ok_status(SecItemAdd(query, NULL), "add internet password");
116 CFReleaseNull(port);
117 }
118
119 /* corrupt all 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, "open keychain");
127
128 char corrupt_item_sql[80];
129 for(int i=1;i<=N_ITEMS;i++) {
130 ok_unix(snprintf(corrupt_item_sql, sizeof(corrupt_item_sql), "UPDATE inet SET data=X'12345678' WHERE rowid=%d", i));
131 is(sqlite3_exec(db, corrupt_item_sql, NULL, NULL, NULL), SQLITE_OK, "corrupting keychain item");
132 }
133 });
134
135 /* start the adder threads */
136 pthread_t add_thread[N_THREADS];
137 void *add_err[N_THREADS] = {NULL,};
138
139 for(int i=0; i<N_THREADS; i++)
140 pthread_create(&add_thread[i], NULL, do_add, (void*)(intptr_t)i);
141
142 /* query the corrupted items */
143 CFDictionaryAddValue(query, kSecReturnPersistentRef, kCFBooleanTrue);
144 for(int i=1;i<=N_ITEMS;i++) {
145 CFTypeRef ref = NULL;
146 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
147 CFDictionarySetValue(query, kSecAttrPort, port);
148 is_status(SecItemCopyMatching(query, &ref), errSecItemNotFound, "Item not found");
149 CFReleaseNull(port);
150 CFReleaseNull(ref);
151 }
152
153 /* collect the adder threads */
154 for(int i=0; i<N_THREADS; i++)
155 pthread_join(add_thread[i], &add_err[i]);
156
157 for(int i=0; i<N_THREADS; i++)
158 ok(add_err[i]==NULL, "add thread");
159
160 CFReleaseNull(pwdata);
161 CFReleaseNull(query);
162 CFReleaseNull(keychain_path_cf);
163 return 0;
164 }