Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Cleanup and sanity-checking of use of strncpy/strcpy (Coverity static analysis)
[palacios.git] / linux_module / iface-code-inject.c
index ff63326..34c3e6a 100644 (file)
@@ -7,7 +7,6 @@
 
 #include <linux/elf.h>
 #include <linux/uaccess.h>
-#include <linux/vmalloc.h>
 
 #include <linux/module.h>
 
@@ -43,8 +42,8 @@ static void free_inject_data (void) {
 
     for(i = 0; i < MAX_INJ; i++) {
         if (top_map[i]) {
-            kfree(top_map[i]->elf_data);
-            kfree(top_map[i]);
+            palacios_free(top_map[i]->elf_data);
+            palacios_free(top_map[i]);
         }
     }
 }
@@ -55,16 +54,17 @@ static int vm_tophalf_inject (struct v3_guest * guest, unsigned int cmd, unsigne
     struct top_half_data top_arg;
     struct top_half_data * top;
 
-    top = kmalloc(sizeof(struct top_half_data), GFP_KERNEL);
-    if (IS_ERR(top)) {
-        printk("Palacios Error: could not allocate space for top half data\n");
+    top = palacios_alloc(sizeof(struct top_half_data));
+    if (!top) {
+        ERROR("Palacios Error: could not allocate space for top half data\n");
         return -EFAULT;
     }
     memset(top, 0, sizeof(struct top_half_data));
     
-    printk("Palacios: Loading ELF data...\n");
+    INFO("Palacios: Loading ELF data...\n");
     if (copy_from_user(&top_arg, (void __user *)arg, sizeof(struct top_half_data))) {
-        printk("palacios: error copying ELF from userspace\n");
+        ERROR("palacios: error copying ELF from userspace\n");
+       palacios_free(top);
         return -EFAULT;
     }
 
@@ -74,36 +74,46 @@ static int vm_tophalf_inject (struct v3_guest * guest, unsigned int cmd, unsigne
 
     /* we have a binary name */
     if (top_arg.is_exec_hooked) {
-        strcpy(top->bin_file, top_arg.bin_file);
+        strncpy(top->bin_file, top_arg.bin_file,256);
+       top->bin_file[255] = 0;
         top->is_exec_hooked = 1;
-        printk("top->bin_file is %s\n", top->bin_file);
+        DEBUG("top->bin_file is %s\n", top->bin_file);
     } 
 
-    printk("Palacios: Allocating %lu B of kernel memory for ELF binary data...\n", top->elf_size);
-    top->elf_data = kmalloc(top->elf_size, GFP_KERNEL);
-    if (IS_ERR(top->elf_data)) {
-        printk("Palacios Error: could not allocate space for binary image\n");
+    DEBUG("Palacios: Allocating %lu B of kernel memory for ELF binary data...\n", top->elf_size);
+    top->elf_data = palacios_alloc(top->elf_size);
+    if (!(top->elf_data)) {
+        ERROR("Palacios Error: could not allocate space for binary image\n");
+       palacios_free(top);
         return -EFAULT;
     }
     memset(top->elf_data, 0, top->elf_size);
 
-    printk("Palacios: Copying ELF image into kernel module...\n");
+    INFO("Palacios: Copying ELF image into kernel module...\n");
     if (copy_from_user(top->elf_data, (void __user *)top_arg.elf_data, top->elf_size)) {
-        printk("Palacios: Error loading elf data\n");
+        ERROR("Palacios: Error loading elf data\n");
+       palacios_free(top->elf_data);
+       palacios_free(top);
         return -EFAULT;
     }
 
-    if (register_top(top) < 0) 
+    if (register_top(top) < 0) {
+       ERROR("Cannot register top half\n");
+       palacios_free(top->elf_data);
+       palacios_free(top);
         return -1;
-    
-    printk("Palacios: setting up inject code...\n");
+    }
+
+    INFO("Palacios: setting up inject code...\n");
     if (v3_insert_code_inject(guest->v3_ctx, top->elf_data, top->elf_size, 
                          top->bin_file, top->is_dyn, top->is_exec_hooked, top->func_offset) < 0) {
-        printk("Palacios Error: error setting up inject code\n");
+        ERROR("Palacios Error: error setting up inject code\n");
+       palacios_free(top->elf_data);
+       palacios_free(top);
         return -1;
     }
 
-    printk("Palacios: injection registration complete\n");
+    INFO("Palacios: injection registration complete\n");
     return 0;
 }
 
@@ -126,6 +136,7 @@ static int guest_init_code_inject (struct v3_guest * guest, void ** vm_data) {
 
 static int guest_deinit_code_inject (struct v3_guest * guest, void * vm_data) {
     free_inject_data();
+    remove_guest_ctrl(guest, V3_VM_TOPHALF_INJECT);
     return 0;
 }