String manipulation in Linux kernel module - linux
I am having a hard time in manipulating strings while writing module for linux. My problem is that I have a int Array[10] with different values in it. I need to produce a string to be able send to the buffer in my_read procedure. If my array is {0,1,112,20,4,0,0,0,0,0}
then my output should be:
0:(0)
1:-(1)
2:-------------------------------------------------------------------------------------------------------(112)
3:--------------------(20)
4:----(4)
5:(0)
6:(0)
7:(0)
8:(0)
9:(0)
when I try to place the above strings in char[] arrays some how weird characters end up there
here is the code
int my_read (char *page, char **start, off_t off, int count, int *eof, void *data)
{
int len;
if (off > 0){
*eof =1;
return 0;
}
/* get process tree */
int task_dep=0; /* depth of a task from INIT*/
get_task_tree(&init_task,task_dep);
char tmp[1024];
char A[ProcPerDepth[0]],B[ProcPerDepth[1]],C[ProcPerDepth[2]],D[ProcPerDepth[3]],E[ProcPerDepth[4]],F[ProcPerDepth[5]],G[ProcPerDepth[6]],H[ProcPerDepth[7]],I[ProcPerDepth[8]],J[ProcPerDepth[9]];
int i=0;
for (i=0;i<1024;i++){ tmp[i]='\0';}
memset(A, '\0', sizeof(A));memset(B, '\0', sizeof(B));memset(C, '\0', sizeof(C));
memset(D, '\0', sizeof(D));memset(E, '\0', sizeof(E));memset(F, '\0', sizeof(F));
memset(G, '\0', sizeof(G));memset(H, '\0', sizeof(H));memset(I, '\0', sizeof(I));memset(J, '\0', sizeof(J));
printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%s\n",A,B,C,D,E,F,G,H,I,J);
memset(A,'-',sizeof(A));
memset(B,'-',sizeof(B));
memset(C,'-',sizeof(C));
memset(D,'-',sizeof(D));
memset(E,'-',sizeof(E));
memset(F,'-',sizeof(F));
memset(G,'-',sizeof(G));
memset(H,'-',sizeof(H));
memset(I,'-',sizeof(I));
memset(J,'-',sizeof(J));
printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%\n",A,B,C,D,E,F,G,H,I,J);
len = sprintf(page,"0:%s(%d)\n1:%s(%d)\n2:%s(%d)\n3:%s(%d)\n4:%s(%d)\n5:%s(%d)\n6:%s(%d)\n7:%s(%d)\n8:%s(%d)\n9:%s(%d)\n",A,ProcPerDepth[0],B,ProcPerDepth[1],C,ProcPerDepth[2],D,ProcPerDepth[3],E,ProcPerDepth[4],F,ProcPerDepth[5],G,ProcPerDepth[6],H,ProcPerDepth[7],I,ProcPerDepth[8],J,ProcPerDepth[9]);
return len;
}
it worked out with this:
char s[500];
memset(s,'-',498);
for (i=len=0;i<10;++i){
len+=sprintf(page+len,"%d:%.*s(%d)\n",i,ProcPerDepth[i],s,ProcPerDepth[i]);
}
I wonder if there is an easy flag to multiply string char in sprintf. thanx –
Here are a some issues:
You have entirely filled the A, B, C ... arrays with characters. Then, you pass them to an I/O routine that is expecting null-terminated strings. Because your strings are not null-terminated, printk() will keep printing whatever is in stack memory after your object until it finds a null by luck.
Multi-threaded kernels like Linux have strict and relatively small constraints regarding stack allocations. All instances in the kernel call chain must fit into a specific size or something will be overwritten. You may not get any detection of this error, just some kind of downstream crash as memory corruption leads to a panic or a wedge. Allocating large and variable arrays on a kernel stack is just not a good idea.
If you are going to write the tmp[] array and properly nul-terminate it, there is no reason to also initialize it. But if you were going to initialize it, you could do so with compiler-generated code by just saying: char tmp[1024] = { 0 }; (A partial initialization of an aggregate requires by C99 initialization of the entire aggregate.) A similar observation applies to the other arrays.
How about getting rid of most of those arrays and most of that code and just doing something along the lines of:
for(i = j = 0; i < n; ++i)
j += sprintf(page + j, "...", ...)
Related
How to add pointer char datas (created using malloc) to a char array in C?
In my MPI code in C, i'm receiving a word from each of my slave processes. I want to add all these words to an char array in master side (part of code below). I can print these words but not collect them into a single char array. (I consider max word length as 10, and number of slave's as slavenumber) char* word = (char*)malloc(sizeof(char)*10); char words[slavenumber*10]; for (int p = 0; p<slavenumber; p++){ MPI_Recv(word, 10, MPI_CHAR, p, 0,MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Word: %s\n", word); //it works fine words[p*10] = *word; //This does not work, i think there is a problem here. } printf(words); //This does not work correctly, it gives something like: ��>;&�>W� Can anybody help me on this?
Let's break it down line by line // allocate a buffer large enough to hold 10 elements of type `char` char* word = (char*)malloc(sizeof(char)*10); // define a variable-length-array large enough to // hold 10*slavenumber elements of `char` char words[slavenumber*10]; for (int p = 0; p<slavenumber; p++){ // dereference `word` which is exactly the same as writing // `word[0]` assigning it to `words[p*10]` words[p*10] = *word; // words[p*10+1] to words[p*10+9] are unchanged, // i.e. uninitialized } // printing from an array. For this to work properly all // accessed elements must be initialized and the buffer // terminated by a null byte. You have neither printf(words); Because you left elements uninitialized and didn't null terminate, you're invoking undefined behavior. Be happy that you didn't get demons crawl out of your nose. In seriousness though, in C you can copy strings by mere assignment. Your usage case calls for strncpy. for (int p = 0; p<slavenumber; p++){ strncpy(&words[p*10], word, 10); }
issue with copy_from_user in kernel
I'm trying to use this function to copy a buffer from the user to one in kernel. both buffers were allocated. I'm using while in case not all the bytes were copied on the first try. but for some reason, nothing is copied and the program is stuck in the while loop. what can be the reasons for that? void my_copy_from_user(const char* source_buff, char* dest_buff, int size_to_copy){ int not_copied = size_to_copy int left = size_to_copy; while( not_copied ){ not_copied = copy_from_user(dest_buff, source_buff, left); dest_buff += (left - not_copied); source_buff += (left - not_copied); left = not_copied; } }
It is possible that it is legitimately failing for reasons that you cannot recover from. Please look at: http://lxr.free-electrons.com/source/arch/x86/lib/usercopy_32.c#L681 unsigned long _copy_from_user(void *to, const void __user *from, unsigned n) { if (access_ok(VERIFY_READ, from, n)) n = __copy_from_user(to, from, n); else memset(to, 0, n); return n; } This is the underlying implementation for copy_from_user for Linux on x86 processors. It first checks access_ok. If access is not allowed, it will fail and return with n (the number of bytes you requested to copy) immediately. This would cause an infinite loop. Two points: I do not think you should invoke copy_from_user in a loop like that. If it fails to copy in kernel mode, there is a reason why. This is a different beast from read() functions when reading from sockets, etc, where you are encouraged to read() in a loop. Are you sure that you are passing in the correct dest_buff to copy_from_user? Tips: Printk all the values and see what's happening. Is left being changed or not? It is likely not.
Is the sscanf function in the Linux kernel susceptible to buffer overflow attacks?
From what I understand, a typical buffer overflow attack occurs when an attack overflows a buffer of memory on the stack, thus allowing the attacker to inject malicious code and rewrite the return address on the stack to point to that code. This is a common concern when using functions (such as sscanf) that blindly copy data from one area to another, checking one for a termination byte: char str[8]; /* holds up to 8 bytes of data */ char *buf = "lots and lots of foobars"; /* way more than 8 bytes of data */ sscanf(buf, "%s", str); /* buffer overflow occurs here! */ I noticed some sysfs_ops store functions in the Linux kernel are implemented with the Linux kernel's version of the sscanf function: static char str[8]; /* global string */ static ssize_t my_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { sscanf(buf, "%s", str); /* buf holds more than 8 bytes! */ return size; } Suppose this store callback function is set to a writable sysfs attribute. Would a malicious user be able to intentionally overflow the buffer via a write call? Normally, I would expect guards against buffer overflow attacks -- such as limiting the number of bytes read -- but I see none in a good number of functions (for example in drivers/scsi/scsi_sysfs.c). Does the implementation of the Linux kernel version of sscanf protect against buffer overflow attacks; or is there another reason -- perhaps buffer overflow attacks are impossible given how the Linux kernel works under the hood?
The Linux sscanf() is vulnerable to buffer overflows; inspection of the source shows this. You can use width specifiers to limit the amount a %s is allowed to write. At some point your str must have had copy_from_user() run on it as well. It is possible the user space to pass some garbage pointer to the kernel. In the version of Linux you cited, the scsi_sysfs.c does have a buffer overflow. The latest version does not. The committed fix should fix the issue you see.
Short answer: sscanf, when well called, will not cause buffer overflow, especially in sysfs xxx_store() function. (There are a lot sscanf in sysfs XXX_store() examples), because Linux kernel add a '\0' (zero-terminated) byte after the string (buf[len] = 0;) for your XXX_store() function. Long answer: Normally, sysfs are defined to have a strict formatted data. Since you expect 8 bytes at most, it's reasonable to limit the size you get like this: static char str[8]; /* global string */ static ssize_t my_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { if (size > 8) { printk("Error: Input size > 8: too large\n"); return -EINVAL; } sscanf(buf, "%s", str); /* buf holds more than 8 bytes! */ return size; } (Note: use 9 rather than 8, if you expect a 8-bytes string plus '\n') (Note that you do reject some inputs such as those with many leading white spaces. However, who would send a string with many leading white spaces? Those who want to break your code, right? If they don't follow your spec, just reject them.) Note that Linux kernel purposely inserts a '\0' at offset len (i.e. buf[len] = 0;) when the user write len bytes to sysfs purposely for safe sscanf, as said in a comment in kernel 2.6: fs/sysfs/file.c: static int fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count) { int error; if (!buffer->page) buffer->page = (char *)get_zeroed_page(GFP_KERNEL); if (!buffer->page) return -ENOMEM; if (count >= PAGE_SIZE) count = PAGE_SIZE - 1; error = copy_from_user(buffer->page,buf,count); buffer->needs_read_fill = 1; /* if buf is assumed to contain a string, terminate it by \0, so e.g. sscanf() can scan the string easily */ buffer->page[count] = 0; return error ? -EFAULT : count; } ... static ssize_t sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct sysfs_buffer * buffer = file->private_data; ssize_t len; mutex_lock(&buffer->mutex); len = fill_write_buffer(buffer, buf, count); if (len > 0) len = flush_write_buffer(file->f_path.dentry, buffer, len); if (len > 0) *ppos += len; mutex_unlock(&buffer->mutex); return len; } Higher kernel version keeps the same logic (though already completely rewritten).
Manipulating strings within structs C
So I have a struct named task that initializes a few things. typedef struct _task{ char *task_name; int priority; // higher numbers have higher priority char date_entered[11]; char date_completed[11]; // not used yet } task; I'm trying to write a function named task *makeTask that takes in (char *name, char *date, and char *priority) I need to allocate memory for the new task, the name within the new task, and I think the date. So far, I've reached a segmentation fault using this: task *makeTask(char *name, char *date, char *priority) { int i; int j; int k; task *newtask = malloc(sizeof(task)); for(i=0; name[i] != '\0'; i++){ if(name[i] == '\n') name[i] = '\0'; } newtask->task_name = malloc(sizeof(char *)*(strlen(name)+1)); strcpy(newtask->task_name, name); newtask->priority = atoi(priority); for(j=0; date[j] != '\0'; j++){ if(date[j] == '\n') date[j] == '\0'; } return newtask;// FILE THIS IN } I think I don't have a really solid understanding of strings and how to manipulate them. Why is my code here giving me a segmentation fault? Is it the loops or the way I've allocated memory for the struct? Any help would be appreciated.
I haven't found anything yet that would guarantee a segmentation fault, but here are some issues. your loops, for(i=0; name[i] != '\0'; i++){ if(name[i] == '\n') name[i] = '\0'; } aside from being somewhat bizarre, You're presuming that name has been properly initialized. If it hasn't been, then that loop will throw a segmentation fault. the same goes for your date loop the following line: newtask->task_name = malloc(sizeof(char )(strlen(name)+1)); sizeof(char *), is a little odd, because you're initializing an array of characters, rather than an array of character pointers(an array of arrays). I don't know if that really makes too much of a difference, but it is odd.
allocating enough memory using typedef struct object whose size varies in another typedef struct
I have defined two typedef structs, and the second has the first as an object: typedef struct { int numFeatures; float* levelNums; } Symbol; typedef struct { int numSymbols; Symbol* symbols; } Data_Set; I then defined numFeatures and numSymbols and allocate memory for both symbols and levelNums, then fill levelNums inside a for loop with value of the inner loop index just to verify it is working as expected. Data_Set lung_cancer; lung_cancer.numSymbols = 5; lung_cancer.symbols = (Symbol*)malloc( lung_cancer.numSymbols * sizeof( Symbol ) ); lung_cancer.symbols->numFeatures = 3; lung_cancer.symbols->levelNums = (float*)malloc( lung_cancer.symbols->numFeatures * sizeof( float ) ); for(int symbol = 0; symbol < lung_cancer.numSymbols; symbol++ ) for( int feature = 0; feature < lung_cancer.symbols->numFeatures; feature++ ) *(lung_cancer.symbols->levelNums + symbol * lung_cancer.symbols->numFeatures + feature ) = feature; for(int symbol = 0; symbol < lung_cancer.numSymbols; symbol++ ) for( int feature = 0; feature < lung_cancer.symbols->numFeatures; feature++ ) cout << *(lung_cancer.symbols->levelNums + symbol * lung_cancer.symbols->numFeatures + feature ) << endl; return 0; When levelNums are int I get what I expect( i.e. 0,1,2,0,1,2,...) but when they are float, only the first 3 are correct and the remaining are very small or very large values, not 0,1,2 like expected. I then have two questions: When allocating memory for symbols, how does it know how big a Symbol is since I have not yet defined how large levelNums will be yet. How do I get float values into levelNums correctly. The reason I am doing it like this is this is a data structure that will be sent to a GPU for GPGPU programming in CUDA and arrays are not recognized. I can only send in a continuous block of memory explicitly and the typedef structs are only there for conveying/defining the memory struture of the data.
A couple thing jump out at meet. For one thing, you only allocated a buffer for levelNums of the first symbol. Similarly, your inner loops always loop over the numFeatures of the first symbol. You're doing a whole lot of dereferencing of arrays, which is fine in general, but the assignment in particular (inside the first set of loops) looks very strange. It's entirely possible I just don't understand what you're trying to do there, but I think it'd be a lot less confusing if you used some square bracket array accessors.