]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-05-corrupted-items.m
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-05-corrupted-items.m
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 * This is to fool os services to not provide the Keychain manager
26 * interface tht doens't work since we don't have unified headers
27 * between iOS and OS X. rdar://23405418/
28 */
29 #define __KEYCHAINCORE__ 1
30
31
32 #import "secd_regressions.h"
33
34 #import <Foundation/Foundation.h>
35 #import <securityd/SecDbItem.h>
36 #import <utilities/array_size.h>
37 #import <utilities/SecCFWrappers.h>
38 #import <utilities/SecFileLocations.h>
39 #import <utilities/fileIo.h>
40
41 #import <securityd/SecItemServer.h>
42
43 #import <Security/SecBasePriv.h>
44
45 #import <AssertMacros.h>
46
47 #import <stdio.h>
48 #import <unistd.h>
49 #import <sys/stat.h>
50 #import <pthread.h>
51
52 #import "SecdTestKeychainUtilities.h"
53
54 #define N_ITEMS (100)
55 #define N_THREADS (10)
56 #define N_ADDS (20)
57
58 static void *do_add(void *arg)
59 {
60 int tid=(int)(arg);
61
62 for(int i=0;i<N_ADDS;i++) {
63 /* Creating a password */
64 SInt32 v_eighty = (tid+1)*1000+i;
65 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
66 const char *v_data = "test";
67 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
68 const void *keys[] = {
69 kSecClass,
70 kSecAttrServer,
71 kSecAttrAccount,
72 kSecAttrPort,
73 kSecAttrProtocol,
74 kSecAttrAuthenticationType,
75 kSecValueData
76 };
77 const void *values[] = {
78 kSecClassInternetPassword,
79 CFSTR("members.spamcop.net"),
80 CFSTR("smith"),
81 eighty,
82 CFSTR("http"),
83 CFSTR("dflt"),
84 pwdata
85 };
86
87 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
88 array_size(keys), NULL, NULL);
89
90 ok_status(SecItemAdd(item, NULL), "add internet password");
91 CFReleaseNull(eighty);
92 CFReleaseNull(pwdata);
93 CFReleaseNull(item);
94 }
95
96 return NULL;
97 }
98
99
100 int secd_05_corrupted_items(int argc, char *const *argv)
101 {
102 plan_tests(1 + N_THREADS*(N_ADDS+1) + N_ITEMS*4 + kSecdTestSetupTestCount);
103
104 /* custom keychain dir */
105 secd_test_setup_temp_keychain("secd_05_corrupted_items", NULL);
106
107 /* add a password */
108 const char *v_data = "test";
109 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
110 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
111 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
112 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("corrupt.spamcop.net"));
113 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
114 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
115 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
116 CFDictionaryAddValue(query, kSecValueData, pwdata);
117
118 SInt32 i;
119 for(i=1; i<=N_ITEMS; i++) {
120 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
121 CFDictionarySetValue(query, kSecAttrPort, port);
122 ok_status(SecItemAdd(query, NULL), "add internet password");
123 CFReleaseNull(port);
124 }
125
126
127
128 SecKeychainDbReset(^{
129 /* corrupt all the password */
130 NSString *keychain_path = CFBridgingRelease(__SecKeychainCopyPath());
131 char corrupt_item_sql[80];
132 sqlite3 *db;
133
134 is(sqlite3_open([keychain_path UTF8String], &db), SQLITE_OK, "open keychain");
135
136 for(int i=1;i<=N_ITEMS;i++) {
137 ok_unix(snprintf(corrupt_item_sql, sizeof(corrupt_item_sql), "UPDATE inet SET data=X'12345678' WHERE rowid=%d", i));
138 is(sqlite3_exec(db, corrupt_item_sql, NULL, NULL, NULL), SQLITE_OK, "corrupting keychain item");
139 }
140 });
141
142 /* start the adder threads */
143 pthread_t add_thread[N_THREADS];
144 void *add_err[N_THREADS] = {NULL,};
145
146 for(int i=0; i<N_THREADS; i++)
147 pthread_create(&add_thread[i], NULL, do_add, (void*)(intptr_t)i);
148
149 /* query the corrupted items */
150 CFDictionaryAddValue(query, kSecReturnPersistentRef, kCFBooleanTrue);
151 for(int i=1;i<=N_ITEMS;i++) {
152 CFTypeRef ref = NULL;
153 CFNumberRef port = CFNumberCreate(NULL, kCFNumberSInt32Type, &i);
154 CFDictionarySetValue(query, kSecAttrPort, port);
155 is_status(SecItemCopyMatching(query, &ref), errSecItemNotFound, "Item not found");
156 CFReleaseNull(port);
157 CFReleaseNull(ref);
158 }
159
160 /* collect the adder threads */
161 for(int i=0; i<N_THREADS; i++)
162 pthread_join(add_thread[i], &add_err[i]);
163
164 for(int i=0; i<N_THREADS; i++)
165 ok(add_err[i]==NULL, "add thread");
166
167 CFReleaseNull(pwdata);
168 CFReleaseNull(query);
169 return 0;
170 }