Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove strcpy, and replace with an iio_strlcpy #439

Closed
wants to merge 4 commits into from

Conversation

rgetz
Copy link
Contributor

@rgetz rgetz commented Apr 16, 2020

We used strcpy a few places, which opens up classic buffer overflow
issues so check those, and fix them.

https://cwe.mitre.org/data/definitions/120.html

This adds some internal error checking, as we are building up xml
representations of things, we now keep track of the remaining space
in the buffer, and don't go over if we have no space. (which in theory
should never happen).

Tested on Pluto and M2k to see if this introduces issues, and couldn't
find anything.

Signed-off-by: Robin Getz [email protected]

@rgetz rgetz force-pushed the rgetz-remove-strcpy branch 2 times, most recently from edf6ce1 to d705f3e Compare April 16, 2020 23:54
@rgetz
Copy link
Contributor Author

rgetz commented Apr 16, 2020

Minor tweaks for Windows, and flawfinder. This does fix 18 issues.

image

@rgetz rgetz requested review from commodo and dNechita April 16, 2020 23:58
@rgetz
Copy link
Contributor Author

rgetz commented Apr 18, 2020

This is part of #441

@rgetz rgetz force-pushed the rgetz-remove-strcpy branch from 3930019 to 67b61fe Compare April 18, 2020 18:44
Copy link
Contributor

@larsclausen larsclausen left a comment

Choose a reason for hiding this comment

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

Hm, not to sure if it is such a good idea to spin a custom version of a secure function.
Might be better to just wrap the existing implementations. On Windows there is strcpy_s.

utilities.c Outdated Show resolved Hide resolved
utilities.c Outdated Show resolved Hide resolved
@rgetz
Copy link
Contributor Author

rgetz commented Apr 20, 2020

The issue that I was trying to avoid is allowing the dest not to be null terminated for some reason.

strncpy : If the destination string of a strcpy() is not large enough, then anything might happen.

strlcpy doesn't exist on linux unless you install libbsd (which seemed overkill for one function).

strncpy_s on Windows has a different function prototype (needs the length to copy, and the length of the dest).

But now that its not the weekend - I get having a standard library function that does not operate like the standard library function. I can just borrow the actual BSD function, and use that:

https://github.com/freebsd/freebsd/blob/master/sys/libkern/strlcpy.c

Right now, In the calling functions, there is no checking to see if things were OK, it just decrements length, and moves on, this is OK as long as length doesn't copy anything when it's negative, but this doesn't comply with the std function - which is how I got here...

Can do either. Suggestions?

rgetz added 4 commits April 20, 2020 15:22
We used strcpy a few places, which opens up classic buffer overflow
issues so check those, and fix them.

https://cwe.mitre.org/data/definitions/120.html

This adds some internal error checking, as we are building up xml
representations of things, we now keep track of the remaining space
in the buffer, and don't go over if we have no space. (which in theory
should never happen).

Tested on Pluto and M2k to see if this introduces issues, and couldn't
find anything.

Signed-off-by: Robin Getz <[email protected]>
According to https://cwe.mitre.org/data/definitions/134.html describes
functions that accepts a format string as an argument, but the format
string originates from an external source as dangerous.

This gets tagged on all the IIO_DEBUG, IIO_INFO, IIO_WARNING, and
IIO_ERRRO functions that are in debug.h However, they are always called
internally from the library, have fixed format strings and can not be
modified externally, so mark them as safe.

Signed-off-by: Robin Getz <[email protected]>
https://cwe.mitre.org/data/definitions/120.html points out that
sprintf will overflow buffers, so don't use it.

We move to the internal iio_snprintf, and move a few remaining snprintf
to the same to handle the necessary windows/Linux differences.

Signed-off-by: Robin Getz <[email protected]>
strncpy is easily used incorrectly since it doesn't always \0-terminate
or check for invalid pointers. However, iio_strlcpy does, so use that.
this is part of CWE-120
https://cwe.mitre.org/data/definitions/120.html

Signed-off-by: Robin Getz <[email protected]>
@rgetz rgetz force-pushed the rgetz-remove-strcpy branch from ba95495 to 5bc984d Compare April 20, 2020 19:23
@rgetz
Copy link
Contributor Author

rgetz commented Apr 20, 2020

This uses a standard strlcpy implementation from BSD.

@rgetz rgetz self-assigned this Apr 20, 2020
size_t *attrs_len, scan_element_len = 0;
unsigned int i;

len = sizeof("<channel id=\"\" type=\"\" ></channel>");
Copy link
Contributor

Choose a reason for hiding this comment

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

for important files like channel.c this patch is doing too much in one go;
i can understand the value of converting

	size_t len = sizeof("<channel id=\"\" name=\"\" "
			"type=\"output\" ></channel>")
		+ strlen(chn->id) + (chn->name ? strlen(chn->name) : 0);

to

	len = sizeof("<channel id=\"\" type=\"\" ></channel>");
	len += strnlen(chn->id, MAX_CHN_ID);
	len += chn->is_output ? sizeof("output") : sizeof("input");
	len += chn->name ? sizeof(" name= ") + strnlen(chn->name, MAX_CHN_NAME) : 0;

but it should be in it's own patch;
otherwise we risk slipping stuff at review;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure - I can split up this commit into multiple.

@pcercuei
Copy link
Contributor

Why not just use strncpy + always clear the last byte?

@rgetz
Copy link
Contributor Author

rgetz commented Apr 21, 2020

Why not just use strncpy + always clear the last byte?

that is basically what strlcpy does. Making a helper function that does that, is close to the same - isn't it?

@rgetz
Copy link
Contributor Author

rgetz commented Apr 21, 2020

I'm going to close this (unmerged) so I can make a new one with a split out version per @commodo 's request.

@rgetz rgetz closed this Apr 21, 2020
@rgetz rgetz mentioned this pull request Apr 21, 2020
@commodo
Copy link
Contributor

commodo commented Apr 23, 2020

can the branch associated with this PR be removed?
should more work branches be removed?

@rgetz rgetz deleted the rgetz-remove-strcpy branch May 20, 2020 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants