Skip to content

Commit

Permalink
Filling return value, still left to return it from IOCTL
Browse files Browse the repository at this point in the history
  • Loading branch information
carmeli-tamir committed Sep 14, 2019
1 parent 561832b commit e1a43e9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
14 changes: 9 additions & 5 deletions TODO
@@ -1,10 +1,14 @@
0. Fix repo.sh: Don't insmod modules if they're loaded
1. Implement return type
1. Implement return type; Left to return it from the ioctl and parse it in python
2. Implement parse_proto_and_call_function_fourByteArg1
3. Implement 2 args function
3. Remove demo code from main
4. Write code to auto - generate function calls
5. write tests?
5. Improve documentation and open project to the public
6. Re-write skunk.py as a class
7. Implement 2 args function
8. write tests
9. Enforce clang formatting
10. Support return values other than 8 bytes


Return: void X u32 X u64 X char* (Return message, each is optional)
Return: void X NUM (32bit / 64bit / ptr) X char* (Return message, each is optional)
Args: char* X u32 X u64 (Repeated proto. Code arguments in Arguments)
80 changes: 38 additions & 42 deletions kernel/call_functions.c
Expand Up @@ -5,12 +5,45 @@

#include "skunk.pb-c.h"

static void parse_proto_and_call_function_ret_64_stringArg1(char *buffer, u32 length, Skunk__ReturnValue *ret)
{
Skunk__FuncWith1Arg *func_1arg;
u32 message_size;
u32 offset = 0;
unsigned long func_addr;

message_size = *((u32*)buffer);
offset = sizeof(message_size);
if (message_size < 0 || message_size > length - offset) {
ret->status = SKUNK__RETURN_VALUE__CALL_STATUS__BadProtobufMessage;
}

func_1arg = skunk__func_with_1_arg__unpack(NULL, message_size, buffer + sizeof(message_size));
if (NULL == func_1arg) {
ret->status = SKUNK__RETURN_VALUE__CALL_STATUS__BadProtobufMessage;
}

func_addr = kallsyms_lookup_name(func_1arg->name);
if (0 == func_addr) {
ret->status = SKUNK__RETURN_VALUE__CALL_STATUS__FunctionDoesntExist;
}
ret->ret64 =(int64_t)((ptrRet64OneArg)func_addr)(func_1arg->arg1);
}

static void parse_proto_and_call_function_ret_64_fourByteArg1(char *buffer, u32 length)
{
pr_info("Hello four byte arg1");
}

long parse_user_buffer_and_call_function(char *buffer, u32 length)
{
Skunk__FunctionType *func_type;
Skunk__ReturnValue ret;
u32 message_size;
u32 offset = 0;

skunk__return_value__init(&ret);

message_size = *((u32*)buffer);
offset = sizeof(message_size);
if (message_size < 0 || message_size > length - offset) {
Expand All @@ -22,62 +55,25 @@ long parse_user_buffer_and_call_function(char *buffer, u32 length)
return -EINVAL;
}

if (func_type->ret != SKUNK__FUNCTION_TYPE__RETURN_TYPE__fourByte) {
//TODO: Handle non 4 bytes return values
if (func_type->ret != SKUNK__FUNCTION_TYPE__RETURN_TYPE__eightByte) {
pr_info("Currently supporting only 4 bytes return value");
skunk__function_type__free_unpacked(func_type, NULL);
return -EINVAL;
}

switch (func_type->args)
{
case SKUNK__FUNCTION_TYPE__ARGUMENTS__stringArg1:
//TODO: Pack return value
parse_proto_and_call_function_stringArg1(buffer + offset + message_size, length - offset);
parse_proto_and_call_function_ret_64_stringArg1(buffer + offset + message_size, length - offset, &ret);
break;
case SKUNK__FUNCTION_TYPE__ARGUMENTS__fourByteArg1:
parse_proto_and_call_function_fourByteArg1(buffer + offset + message_size, length - offset);
parse_proto_and_call_function_ret_64_fourByteArg1(buffer + offset + message_size, length - offset);
break;
default:
break;
}

return 0;
}


u32 parse_proto_and_call_function_stringArg1(char *buffer, u32 length)
{
Skunk__FuncWith1Arg *func_1arg;
u32 message_size;
u32 offset = 0;
u64 ret = 0;
unsigned long func_addr;

message_size = *((u32*)buffer);
offset = sizeof(message_size);
if (message_size < 0 || message_size > length - offset) {
return message_size;
}

func_1arg = skunk__func_with_1_arg__unpack(NULL, message_size, buffer + sizeof(message_size));
if (NULL == func_1arg) {
return -EINVAL;
}

func_addr = kallsyms_lookup_name(func_1arg->name);
if (0 == func_addr) {
return -EINVAL;
}

ret =(u64) ((ptrRet64OneArg)func_addr)(func_1arg->arg1);

pr_info("Got ret value of %p", (void*)ret);
skunk__function_type__free_unpacked(func_type, NULL);

return 0;
}

u32 parse_proto_and_call_function_fourByteArg1(char *buffer, u32 length)
{
pr_info("Hello four byte arg1");
return 0;
}
4 changes: 0 additions & 4 deletions kernel/skunk.h
Expand Up @@ -13,8 +13,4 @@ typedef u64 (*ptrRet64OneArg)(void *arg1);

long parse_user_buffer_and_call_function(char *buffer, u32 length);

u32 parse_proto_and_call_function_stringArg1(char *buffer, u32 length);

u32 parse_proto_and_call_function_fourByteArg1(char *buffer, u32 length);

#endif /* SKUNK_H */
1 change: 1 addition & 0 deletions kernel/skunk_device.c
Expand Up @@ -33,6 +33,7 @@ static long ioctl_skunk_device(struct file *file, unsigned int cmd, unsigned lon
return -ENOMEM;
}
if (copy_from_user(message, ((char*)arg ) + sizeof(message_size), message_size)) {
kfree(message);
return -ENOMEM;
}
ret = parse_user_buffer_and_call_function(message, message_size);
Expand Down
8 changes: 4 additions & 4 deletions skunk.proto
Expand Up @@ -9,7 +9,7 @@ message function_type {
}

enum ReturnType {
fourByte = 0;
eightByte = 0;
}
required Arguments args = 1;
required ReturnType ret = 2;
Expand All @@ -19,11 +19,11 @@ message ReturnValue {
enum CallStatus {
Success = 0;
FunctionDoesntExist = 1;
BadProtobufMessage = 2;
}
required CallStatus status = 1;
optional int32 ret32 = 2;
optional int64 ret64 = 3;
optional string retString = 4;
optional int64 ret64 = 2;
optional string retString = 3;
}

message func_with_1_arg {
Expand Down
2 changes: 1 addition & 1 deletion user/skunk.py
Expand Up @@ -25,7 +25,7 @@ def call_function_demo(device, fname, fargs, fret, arg1):

def run_skunk():
with open("/dev/skunk", 'r') as skunk_device:
call_function_demo(skunk_device, "kallsyms_lookup_name", skunk_pb2.function_type.stringArg1, skunk_pb2.function_type.fourByte, "kallsyms_lookup_name")
call_function_demo(skunk_device, "kallsyms_lookup_name", skunk_pb2.function_type.stringArg1, skunk_pb2.function_type.eightByte, "kallsyms_lookup_name")


if __name__ == "__main__":
Expand Down

0 comments on commit e1a43e9

Please sign in to comment.