-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.diff
More file actions
334 lines (327 loc) · 10.3 KB
/
patch.diff
File metadata and controls
334 lines (327 loc) · 10.3 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
From f98a894807cf7197a32c3035f8b5be2748b718df Mon Sep 17 00:00:00 2001
From: joshilp <joshilp@sfu.ca>
Date: Thu, 23 Nov 2017 17:00:13 -0800
Subject: [PATCH] committing new kernel
---
Makefile | 2 +-
arch/x86/entry/syscalls/syscall_64.tbl | 3 +
cs300/Makefile | 1 +
cs300/array_stats.c | 54 ++++++++++++
cs300/array_stats.h | 9 ++
cs300/cs300_test.c | 16 ++++
cs300/process_ancestors.c | 147 +++++++++++++++++++++++++++++++++
cs300/process_ancestors.h | 15 ++++
8 files changed, 246 insertions(+), 1 deletion(-)
create mode 100755 cs300/Makefile
create mode 100644 cs300/array_stats.c
create mode 100644 cs300/array_stats.h
create mode 100755 cs300/cs300_test.c
create mode 100644 cs300/process_ancestors.c
create mode 100644 cs300/process_ancestors.h
diff --git a/Makefile b/Makefile
index ccd9818..85241e6 100644
--- a/Makefile
+++ b/Makefile
@@ -946,7 +946,7 @@ endif
ifeq ($(KBUILD_EXTMOD),)
-core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/
+core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/ cs300/
vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
$(core-y) $(core-m) $(drivers-y) $(drivers-m) \
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index 5aef183..6e684f4 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -339,6 +339,9 @@
330 common pkey_alloc sys_pkey_alloc
331 common pkey_free sys_pkey_free
332 common statx sys_statx
+340 common cs300_test sys_cs300_test
+341 common array_stats sys_array_stats
+342 common process_ancestors sys_process_ancestors
#
# x32-specific system call numbers start at 512 to avoid cache impact
diff --git a/cs300/Makefile b/cs300/Makefile
new file mode 100755
index 0000000..907f0b6
--- /dev/null
+++ b/cs300/Makefile
@@ -0,0 +1 @@
+obj-y := cs300_test.o array_stats.o process_ancestors.o
\ No newline at end of file
diff --git a/cs300/array_stats.c b/cs300/array_stats.c
new file mode 100644
index 0000000..6421e85
--- /dev/null
+++ b/cs300/array_stats.c
@@ -0,0 +1,54 @@
+#include <linux/kernel.h>
+#include <linux/uaccess.h>
+#include "array_stats.h"
+
+// stats: A pointer to one array_stats structure allocated by the user-space application.
+// Structure will be written to by the sys-call to store the minimum, maximum, and sum of all
+// values in the array pointed to by data.
+// data: An array of long int values passed in by the user-space application.
+// size: The number of elements in data. Must be > 0.
+
+asmlinkage long sys_array_stats(struct array_stats *stats,long data[],long size)
+{
+ long test; // variable for data array elements
+ long i; // index
+ struct array_stats kernel_stats; // kernel stats structure
+
+ if (size <= 0){
+ return -EINVAL;
+ }
+
+ for (i=0;i<size;i++){
+ // have problems access stats or data.
+ if (copy_from_user(&test, &data[i], sizeof(data[i])) != 0){
+ return -EFAULT;
+ }
+ // initialize kernel stats
+ if (i == 0)
+ {
+ kernel_stats.min = test;
+ kernel_stats.max = test;
+ kernel_stats.sum = 0;
+ }
+ // find the min
+ if (test < kernel_stats.min){
+ kernel_stats.min = test;
+ }
+ // find the max
+ if (test > kernel_stats.max){
+ kernel_stats.max = test;
+ }
+ // sum
+ kernel_stats.sum += test;
+ }
+
+ // copy back to user
+ if (copy_to_user(stats,&kernel_stats,sizeof(kernel_stats)) != 0){
+ return -EFAULT;
+ }
+
+
+ // successful, return 0
+ return 0;
+}
+
diff --git a/cs300/array_stats.h b/cs300/array_stats.h
new file mode 100644
index 0000000..653d940
--- /dev/null
+++ b/cs300/array_stats.h
@@ -0,0 +1,9 @@
+// Define the array_stats struct for the array_stats sys-call.
+#ifndef _ARRAY_STATS_H_
+#define _ARRAY_STATS_H_
+struct array_stats{
+ long min;
+ long max;
+ long sum;
+};
+#endif
\ No newline at end of file
diff --git a/cs300/cs300_test.c b/cs300/cs300_test.c
new file mode 100755
index 0000000..951ac943
--- /dev/null
+++ b/cs300/cs300_test.c
@@ -0,0 +1,16 @@
+#include <linux/kernel.h>
+
+// Implement a HelloWorld system call
+// Argument is passed from call in user space.
+asmlinkage long sys_cs300_test(int argument)
+{
+ long result = 0;
+
+ printk("Hello World!\n");
+ printk("--syscall argument %d\n", argument);
+
+ result = argument + 1;
+ printk("--returning %d + 1 = %ld\n", argument, result);
+ return result;
+}
+
diff --git a/cs300/process_ancestors.c b/cs300/process_ancestors.c
new file mode 100644
index 0000000..f9e22cf
--- /dev/null
+++ b/cs300/process_ancestors.c
@@ -0,0 +1,147 @@
+#include <linux/sched.h>
+#include <linux/string.h>
+#include <linux/kernel.h>
+#include <linux/uaccess.h>
+#include <linux/unistd.h>
+#include <linux/cred.h>
+#include "process_ancestors.h"
+
+// info_array[]: An array of process_info structs that will be written to by the kernel
+// as it fills in information from the current process on up through its ancestors.
+// size: The number of structs in info_array[]. This is the maximum number of structs
+// that the kernel will write into the array (starting with the current process as the first
+// entry and working up from there). The size may be larger or smaller than the actual
+// number of ancestors of the current process: larger means some entries are left unused
+// (see num_filled); smaller means information about some processes not written into the
+// array.
+// num_filled: A pointer to a long. To this location the kernel will store the number of
+// structs (in info_array[]) which are written by the kernel. May be less than size if the
+// number of ancestors for the current process is less than size.
+
+asmlinkage long sys_process_ancestors(struct process_info info_array[],long size,long *num_filled){
+
+ struct task_struct *info; // get info for each array element
+ struct process_info process; // each array element
+ long children=0; // number of children
+ long sibling=0; // number of sibling
+ long i=0; // index
+ struct list_head *childPointer; // kernel link list for child
+ struct list_head *siblingPointer; // kernel link list for sibling
+ struct task_struct *child_task;
+ struct task_struct *sibling_task;
+ if (size <= 0){
+ return -EINVAL;
+ }
+ if (info_array == NULL){
+ return -EINVAL;
+ }
+
+ info = current;
+ // printk("info->pid: %ld\n", (long) info->pid);
+ // printk("info->name: %s\n", info->comm);
+ // printk("info->state: %ld\n", info->state);
+ // printk("info->uid: %ld\n", (long)info->cred->uid.val);
+ // printk("info->nvcsw: %ld\n", info->nvcsw);
+ // printk("info->nivcsw: %ld\n", info->nivcsw);
+
+
+ // childPointer = &info->children;
+
+ // printk("pointer is %p\n", childPointer);
+
+ // list_for_each(childPointer, &info->children) {
+ // //printk("pointer is %p\n",childPointer);
+ // child_task = list_entry(childPointer,struct task_struct,children);
+ // children++;
+ // //printk("child is %ld\n",children);
+ // }
+
+ // printk("children is %ld\n", children);
+
+ // list_for_each(siblingPointer, &info->sibling) {
+ // sibling_task = list_entry(siblingPointer,struct task_struct,sibling);
+ // sibling++;
+ // //printk("sibling is %ld\n",sibling);
+ // }
+ // printk(" sbling is %ld\n", sibling);
+ // list_for_each(childPointer, &info->children) {
+ //printk("pointer is %p\n",childPointer);
+ // child_task = list_entry(childPointer,struct task_struct,sibling);
+ // children++;
+ // //printk("child is %ld\n",children);
+ // }
+
+ while(info->parent != info){
+
+ process.pid = info->pid; // get the process id
+
+ //printk("1.pid is ok\n");
+
+ //memset(process.name, '\0', sizeof(process.name));
+
+ strncpy(process.name, info->comm, 16); // copy the process name
+ //printk("2.name is ok\n");
+
+ process.state = info->state; // get the process state
+ //printk("3.state is ok\n");
+
+ process.uid = info->cred->uid.val; // get the uid
+ //printk("4.uid is ok\n");
+
+
+ process.nvcsw = info->nvcsw;
+
+ process.nivcsw = info->nivcsw;
+
+ //printk("5.nv is ok\n");
+
+ // count the child process
+ list_for_each(childPointer, &info->children) {
+ //printk("pointer is %p\n",childPointer);
+ child_task = list_entry(childPointer,struct task_struct,sibling);
+ children++;
+ //printk("child is %ld\n",children);
+ }
+
+ // count the sibling process
+ list_for_each(siblingPointer, &info->sibling) {
+ sibling_task = list_entry(siblingPointer,struct task_struct,sibling);
+ sibling++;
+ //printk("sibling is %ld\n",sibling);
+ }
+
+ process.num_children = children;
+ process.num_siblings = sibling;
+
+ // printk("children2 is %ld\n",children);
+ // printk("sbling2 is %ld\n",sibling);
+
+ //info_array[i] = process; // assign the value back to array
+
+ if (!access_ok(VERIFY_WRITE, &info_array[i], sizeof(process))){
+ return -EFAULT;
+ }
+
+ if (copy_to_user(&info_array[i], &process, sizeof(process) ) != 0){
+ return -EINVAL;
+ }
+
+
+
+ //printk("get here\n");
+ i++;
+ // printk("info->num_children: %ld\n", children);
+ // printk("info->num_siblings: %ld\n", sibling);
+
+ info = info->parent; // go to the parent process
+ }
+
+
+ *num_filled = i; // the number of filled is iteration
+
+ //printk("num is %ld\n",*num_filled);
+
+ // printk("after while loop\n");
+
+ return 0;
+}
\ No newline at end of file
diff --git a/cs300/process_ancestors.h b/cs300/process_ancestors.h
new file mode 100644
index 0000000..285e2a8
--- /dev/null
+++ b/cs300/process_ancestors.h
@@ -0,0 +1,15 @@
+// Structure to hold values returned by process_ancestors sys-call
+#ifndef _PROCESS_ANCESTORS_H
+#define _PROCESS_ANCESTORS_H
+#define ANCESTOR_NAME_LEN 16
+struct process_info {
+ long pid; /* Process ID */
+ char name[ANCESTOR_NAME_LEN]; /* Program name of process */
+ long state; /* Current process state */
+ long uid; /* User ID of process owner */
+ long nvcsw; /* # voluntary context switches */
+ long nivcsw; /* # involuntary context switches */
+ long num_children; /* # children process has */
+ long num_siblings; /* # sibling process has */
+};
+#endif
\ No newline at end of file
--
2.7.4