Skip to content

Task10#161

Open
ant112342 wants to merge 2 commits intoKernel-GL-HRK:anton.kotsiubailofrom
ant112342:task10
Open

Task10#161
ant112342 wants to merge 2 commits intoKernel-GL-HRK:anton.kotsiubailofrom
ant112342:task10

Conversation

@ant112342
Copy link

Done task10

ekovalyov and others added 2 commits March 28, 2021 16:53
Implemented code for task10, where was
realized characted device.

Signen-off-by: Anton Kotsiubailo <antohakotsubailo@gmail.com>
@ant112342 ant112342 added the Ready for review Ready for review label Mar 28, 2021
@ant112342 ant112342 requested a review from Anton-Soroka March 28, 2021 14:00
static void cleanup_buffer(char *buff)
{
kfree(buff);
buff = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line have no effect, it just clears local pointer buff.
If you want to clear user passed pointer - then you need to pass pointer of the pointer to this function.

Comment on lines +96 to +97
if (volume_buffer == NULL)
return -ENOMEM;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper error handling!
In case of error - need to free previously allocated resources in order to prevent potential memory leak.

proc_file = proc_create(PROC_FILENAME_1, S_IFREG | 0444, proc_dir,
&volume_fops);
if (proc_file == NULL)
return -EFAULT;
Copy link

@Anton-Soroka Anton-Soroka Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper error handling! In case of error - need to free previously allocated resources in order to prevent potential memory leak and system crash.

proc_file2 = proc_create(PROC_FILENAME_2, S_IFREG | 0444, proc_dir,
&size_fops);
if (proc_file == NULL)
return -EFAULT;
Copy link

@Anton-Soroka Anton-Soroka Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper error handling! In case of error - need to free previously allocated resources in order to prevent potential memory leak and system crash.

{
if (proc_file2) {
remove_proc_entry(PROC_FILENAME_2, proc_dir);
proc_file = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? Did you mean to clear proc_file2 variable here?

Comment on lines +52 to +65
static struct file_operations volume_fops = {
.read = volume_read,
};

static struct file_operations size_fops = {
.read = size_read,
};

static struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkpatch warning: these structs should be const!
image

Comment on lines +236 to +237
int err;
if (length > LENGTH_DEF) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkpatch warning:
image


message = kmalloc(length * sizeof(char), GFP_KERNEL);
if (message == NULL)
return -ENOMEM;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper error handling.

return 0;
}

left = copy_to_user(buffer, volume_buffer, len);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line contains mistake that lead to garbage in function output.
Please read documentation on sprintf and copy_to_user functions and then fix the mistake.
image

return 0;
}

left = copy_to_user(buffer, size_buffer, len);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same mistake as in volume_read.

Comment on lines +345 to +346
if (len > sizeof(message) - 1)
size_of_message = sizeof(message) - 1;
Copy link

@Anton-Soroka Anton-Soroka Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to your code, message is a pointer.
What would be the value of sizeof(message) ?

if (len > sizeof(message) - 1)
size_of_message = sizeof(message) - 1;

ret = copy_from_user(message, buffer, len);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you use "len" here, it means that previous length-check has no effect.
Out of bound read is a security issue and potential system crash.


proc_file2 = proc_create(PROC_FILENAME_2, S_IFREG | 0444, proc_dir,
&size_fops);
if (proc_file == NULL)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? Did you mean to check proc_file2 here?

class_destroy(charClass);
unregister_chrdev(majorNumber, DEVICE_NAME);
kfree(message);
cleanup_proc();
Copy link

@Anton-Soroka Anton-Soroka Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this function, because it is the reason for error message in system log:
image

Copy link

@Anton-Soroka Anton-Soroka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need fixes.

@Anton-Soroka Anton-Soroka added Change requested Change requested and removed Ready for review Ready for review labels Mar 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Change requested Change requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants