1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,osx.MIG -std=c++14 \
// RUN: -analyzer-output=text -fblocks -verify %s
typedef unsigned uint32_t;
// XNU APIs.
typedef int kern_return_t;
#define KERN_SUCCESS 0
#define KERN_ERROR 1
#define MIG_NO_REPLY (-305)
typedef unsigned mach_port_name_t;
typedef unsigned vm_address_t;
typedef unsigned vm_size_t;
typedef void *ipc_space_t;
typedef unsigned long io_user_reference_t;
typedef struct ipc_port *ipc_port_t;
typedef unsigned mach_port_t;
typedef uint32_t UInt32;
struct os_refcnt {};
typedef struct os_refcnt os_refcnt_t;
struct thread {
os_refcnt_t ref_count;
};
typedef struct thread *thread_t;
kern_return_t vm_deallocate(mach_port_name_t, vm_address_t, vm_size_t);
kern_return_t mach_vm_deallocate(mach_port_name_t, vm_address_t, vm_size_t);
void mig_deallocate(vm_address_t, vm_size_t);
kern_return_t mach_port_deallocate(ipc_space_t, mach_port_name_t);
void ipc_port_release(ipc_port_t);
void thread_deallocate(thread_t);
static void os_ref_retain(struct os_refcnt *rc);
#define thread_reference_internal(thread) os_ref_retain(&(thread)->ref_count);
#define MIG_SERVER_ROUTINE __attribute__((mig_server_routine))
// IOKit wrappers.
class OSObject;
typedef kern_return_t IOReturn;
#define kIOReturnError 1
enum {
kOSAsyncRef64Count = 8,
};
typedef io_user_reference_t OSAsyncReference64[kOSAsyncRef64Count];
struct IOExternalMethodArguments {
io_user_reference_t *asyncReference;
};
struct IOExternalMethodDispatch {};
class IOUserClient {
public:
static IOReturn releaseAsyncReference64(OSAsyncReference64);
static IOReturn releaseNotificationPort(mach_port_t port);
MIG_SERVER_ROUTINE
virtual IOReturn externalMethod(
uint32_t selector, IOExternalMethodArguments *arguments,
IOExternalMethodDispatch *dispatch = 0, OSObject *target = 0,
void *reference = 0);
MIG_SERVER_ROUTINE
virtual IOReturn registerNotificationPort(mach_port_t, UInt32, UInt32);
};
// Tests.
MIG_SERVER_ROUTINE
kern_return_t basic_test(mach_port_name_t port, vm_address_t address, vm_size_t size) {
vm_deallocate(port, address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
if (size > 10) { // expected-note{{Assuming 'size' is > 10}}
// expected-note@-1{{Taking true branch}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
}
return KERN_SUCCESS;
}
MIG_SERVER_ROUTINE
kern_return_t test_unknown_return_value(mach_port_name_t port, vm_address_t address, vm_size_t size) {
extern kern_return_t foo();
vm_deallocate(port, address, size);
// We don't know if it's a success or a failure.
return foo(); // no-warning
}
// Make sure we don't crash when they forgot to write the return statement.
MIG_SERVER_ROUTINE
kern_return_t no_crash(mach_port_name_t port, vm_address_t address, vm_size_t size) {
vm_deallocate(port, address, size);
}
// When releasing two parameters, add a note for both of them.
// Also when returning a variable, explain why do we think that it contains
// a non-success code.
MIG_SERVER_ROUTINE
kern_return_t release_twice(mach_port_name_t port, vm_address_t addr1, vm_address_t addr2, vm_size_t size) {
kern_return_t ret = KERN_ERROR; // expected-note{{'ret' initialized to 1}}
vm_deallocate(port, addr1, size); // expected-note{{Value passed through parameter 'addr1' is deallocated}}
vm_deallocate(port, addr2, size); // expected-note{{Value passed through parameter 'addr2' is deallocated}}
return ret; // expected-warning{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
}
MIG_SERVER_ROUTINE
kern_return_t no_unrelated_notes(mach_port_name_t port, vm_address_t address, vm_size_t size) {
vm_deallocate(port, address, size); // no-note
1 / 0; // expected-warning{{Division by zero}}
// expected-note@-1{{Division by zero}}
return KERN_SUCCESS;
}
// Make sure we find the bug when the object is destroyed within an
// automatic destructor.
MIG_SERVER_ROUTINE
kern_return_t test_vm_deallocate_in_automatic_dtor(mach_port_name_t port, vm_address_t address, vm_size_t size) {
struct WillDeallocate {
mach_port_name_t port;
vm_address_t address;
vm_size_t size;
~WillDeallocate() {
vm_deallocate(port, address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
}
} will_deallocate{port, address, size};
if (size > 10) {
// expected-note@-1{{Assuming 'size' is > 10}}
// expected-note@-2{{Taking true branch}}
return KERN_ERROR;
// expected-note@-1{{Calling '~WillDeallocate'}}
// expected-note@-2{{Returning from '~WillDeallocate'}}
// expected-warning@-3{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
// expected-note@-4 {{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
}
return KERN_SUCCESS;
}
// Check that we work on Objective-C messages and blocks.
@interface I
- (kern_return_t)fooAtPort:(mach_port_name_t)port withAddress:(vm_address_t)address ofSize:(vm_size_t)size;
@end
@implementation I
- (kern_return_t)fooAtPort:(mach_port_name_t)port
withAddress:(vm_address_t)address
ofSize:(vm_size_t)size MIG_SERVER_ROUTINE {
vm_deallocate(port, address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
}
@end
void test_block() {
kern_return_t (^block)(mach_port_name_t, vm_address_t, vm_size_t) =
^MIG_SERVER_ROUTINE (mach_port_name_t port,
vm_address_t address, vm_size_t size) {
vm_deallocate(port, address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
};
}
void test_block_with_weird_return_type() {
struct Empty {};
// The block is written within a function so that it was actually analyzed as
// a top-level function during analysis. If we were to write it as a global
// variable of block type instead, it would not have been analyzed, because
// ASTConsumer won't find the block's code body within the VarDecl.
// At the same time, we shouldn't call it from the function, because otherwise
// it will be analyzed as an inlined function rather than as a top-level
// function.
Empty (^block)(mach_port_name_t, vm_address_t, vm_size_t) =
^MIG_SERVER_ROUTINE(mach_port_name_t port,
vm_address_t address, vm_size_t size) {
vm_deallocate(port, address, size);
return Empty{}; // no-crash
};
}
// Test various APIs.
MIG_SERVER_ROUTINE
kern_return_t test_mach_vm_deallocate(mach_port_name_t port, vm_address_t address, vm_size_t size) {
mach_vm_deallocate(port, address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
MIG_SERVER_ROUTINE
kern_return_t test_mach_port_deallocate(ipc_space_t space,
mach_port_name_t port) {
mach_port_deallocate(space, port); // expected-note{{Value passed through parameter 'port' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
MIG_SERVER_ROUTINE
kern_return_t test_mig_deallocate(vm_address_t address, vm_size_t size) {
mig_deallocate(address, size); // expected-note{{Value passed through parameter 'address' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
MIG_SERVER_ROUTINE
kern_return_t test_ipc_port_release(ipc_port_t port) {
ipc_port_release(port); // expected-note{{Value passed through parameter 'port' is deallocated}}
return KERN_ERROR; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
// Let's try the C++11 attribute spelling syntax as well.
[[clang::mig_server_routine]]
IOReturn test_releaseAsyncReference64(IOExternalMethodArguments *arguments) {
IOUserClient::releaseAsyncReference64(arguments->asyncReference); // expected-note{{Value passed through parameter 'arguments' is deallocated}}
return kIOReturnError; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
MIG_SERVER_ROUTINE
kern_return_t test_no_reply(ipc_space_t space, mach_port_name_t port) {
mach_port_deallocate(space, port);
return MIG_NO_REPLY; // no-warning
}
class MyClient: public IOUserClient {
// The MIG_SERVER_ROUTINE annotation is intentionally skipped.
// It should be picked up from the superclass.
IOReturn externalMethod(uint32_t selector, IOExternalMethodArguments *arguments,
IOExternalMethodDispatch *dispatch = 0, OSObject *target = 0, void *reference = 0) override {
releaseAsyncReference64(arguments->asyncReference); // expected-note{{Value passed through parameter 'arguments' is deallocated}}
return kIOReturnError; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
IOReturn registerNotificationPort(mach_port_t port, UInt32 x, UInt32 y) {
releaseNotificationPort(port); // expected-note{{Value passed through parameter 'port' is deallocated}}
return kIOReturnError; // expected-warning{{MIG callback fails with error after deallocating argument value}}
// expected-note@-1{{MIG callback fails with error after deallocating argument value}}
}
};
MIG_SERVER_ROUTINE
kern_return_t test_os_ref_retain(thread_t thread) {
thread_reference_internal(thread);
thread_deallocate(thread);
return KERN_ERROR; // no-warning
}
|