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

use strndup/memdup instead of malloc, memcpy and null-terminate etc #12453

Closed
wants to merge 2 commits into from

Conversation

bagder
Copy link
Member

@bagder bagder commented Dec 4, 2023

No description provided.

@bagder bagder changed the title rtsp: use strndup instead of malloc, memcpy and null-terminate strndup/memdup instead of malloc, memcpy and null-terminate etc Dec 4, 2023
@bagder bagder changed the title strndup/memdup instead of malloc, memcpy and null-terminate etc use strndup/memdup instead of malloc, memcpy and null-terminate etc Dec 4, 2023
@bagder
Copy link
Member Author

bagder commented Dec 4, 2023

@jay: I want to remove the memchr stuff again from strndup() because I think it potentially breaks several of these strndup cases.

@bagder bagder removed the RTSP label Dec 4, 2023
It makes it possible to clone a binary chunk of data.
 - bufref: use strndup
 - cookie: use strndup
 - formdata: use strndup
 - ftp: use strndup
 - gtls: use aprintf instead of malloc + strcpy * 2
 - http: use strndup
 - mbedtls: use strndup
 - md4: use memdup
 - ntlm: use memdup
 - ntlm_sspi: use strndup
 - pingpong: use memdup
 - rtsp: use strndup instead of malloc, memcpy and null-terminate
 - sectransp: use strndup
 - socks_gssapi.c: use memdup
 - vtls: use dynbuf instead of malloc, snprintf and memcpy
 - vtls: use strdup instead of malloc + memcpy
 - wolfssh: use strndup

Closes #12453
Comment on lines 115 to 116
if(!buf)
return NULL;
Copy link
Member

Choose a reason for hiding this comment

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

I want to remove the memchr stuff again from strndup() because I think it potentially breaks several of these strndup cases.

How is it breaking? If we don't check if the length of src is shorter than the passed in length then the memcpy can read past the end of src. If the full passed in length needs to be allocated then it could be adjusted after the allocation but memchr would need to be used:

Suggested change
if(!buf)
return NULL;
char *end;
if(!buf)
return NULL;
end = memchr(src, '\0', length);
if(end)
length = end - src;

Copy link
Member Author

Choose a reason for hiding this comment

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

Because nowhere in the code does it assume that it should only add the substring up to a null terminator. They all want the specified subtring allocated, with a null terminator so that it can be treated as a string.

In some cases the substring might contain a null byte and then the added null terminator is of course unnecessary but that's how the code already works. If the memchr() logic remains , this use case breaks.

Copy link
Member

@jay jay Dec 6, 2023

Choose a reason for hiding this comment

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

Can you give me an example? I still don't get it.

edit: If you mean the rest of the memory should be cleared then use calloc instead of malloc?

Copy link
Member Author

Choose a reason for hiding this comment

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

We want:

  1. alloc N bytes + one byte null termation
  2. copy N bytes (not to the nearest null byte) to the target memory

Not one of these cases want the copy to stop at the first null byte.

See the sectransp.c, ntlm_sspi.c and mbedtls.c changes. Scanning for null bytes in there is just wasteful or at worst wrong.

@bagder bagder closed this in 63cdaef Dec 7, 2023
bagder added a commit that referenced this pull request Dec 7, 2023
 - bufref: use strndup
 - cookie: use strndup
 - formdata: use strndup
 - ftp: use strndup
 - gtls: use aprintf instead of malloc + strcpy * 2
 - http: use strndup
 - mbedtls: use strndup
 - md4: use memdup
 - ntlm: use memdup
 - ntlm_sspi: use strndup
 - pingpong: use memdup
 - rtsp: use strndup instead of malloc, memcpy and null-terminate
 - sectransp: use strndup
 - socks_gssapi.c: use memdup
 - vtls: use dynbuf instead of malloc, snprintf and memcpy
 - vtls: use strdup instead of malloc + memcpy
 - wolfssh: use strndup

Closes #12453
@bagder bagder deleted the bagder/rtsp-strndup branch December 7, 2023 07:48
@@ -116,12 +117,9 @@ CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);

if(ptr) {
cpy = malloc(len + 1);
cpy = Curl_strndup(ptr, len);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is wrong: the function is supposed to duplicate the given byte count, but this will stop at the first null byte.
The original code behaves more like memdup, but appends a null byte as a sentinel in case the data is effectively a string.
Maybe we should have a such a generic dual-use duplication function.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, Curl_strndup() does not stop at the first null byte (any more). That is exactly what I mentioned above in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad! I've been abused by the Curl_strndup() original code and the usage semantics associated with this name.
Sorry for the noise.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've been pondering on maybe renaming this function just to make it less likely that one would think that it has this functionality...

Copy link
Contributor

Choose a reason for hiding this comment

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

... one would think that it has this functionality...

This happened to me: will probably affect others.

@icing
Copy link
Contributor

icing commented Dec 8, 2023 via email

@bagder
Copy link
Member Author

bagder commented Dec 8, 2023

I made #12490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

4 participants