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

Can we remove usage of memcpy? #7660

Closed
craxal opened this issue Jan 9, 2024 · 4 comments
Closed

Can we remove usage of memcpy? #7660

craxal opened this issue Jan 9, 2024 · 4 comments
Assignees
Labels
✅ merged A fix for this issue has been merged
Milestone

Comments

@craxal
Copy link
Contributor

craxal commented Jan 9, 2024

clipboard-files uses memcpy, which is not an API we should be using.

The most likely fix to this is to use std::string instead of std::shared_ptr<unsigned char>.

https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1864180

@JasonYeMSFT
Copy link
Contributor

We need to directly manipulate memory to parse the file paths out of consecutive memory chunks. Using string doesn't help much because at the end of the day, we still need to tell a string constructor to take bytes from a given address with a size and, like memcpy, the std::string constructor doesn't check the boundary of the source buffer. For example, the following code can be compiled and will result in undefined behavior:

char x[] = "hello"; // 6 bytes including the null terminator
std::string s(x, 7); // Construct the string with 1 byte overflow
std::cout << s << std::endl;

The memcpy usage in our code are only for platforms where the secure alternatives aren't available. They also have size validation that ensures neither the source nor the destination buffer would overflow.

@JasonYeMSFT JasonYeMSFT added the ✅ won't fix Will not be worked on label Jan 9, 2024
@craxal
Copy link
Contributor Author

craxal commented Jan 10, 2024

@JasonYeMSFT If there are no alternatives, we need to make sure that we've used the proper annotations and reviewed with security team. We're not done until then.

@craxal craxal removed the ✅ won't fix Will not be worked on label Jan 10, 2024
@craxal craxal reopened this Jan 10, 2024
@MRayermannMSFT MRayermannMSFT added the ❔ investigate We need to look into this further label Jan 22, 2024
@MRayermannMSFT MRayermannMSFT added this to the 1.33.0 milestone Jan 22, 2024
@MRayermannMSFT MRayermannMSFT changed the title Remove usage of memcpy Can we remove usage of memcpy? Jan 22, 2024
@JasonYeMSFT
Copy link
Contributor

JasonYeMSFT commented Jan 24, 2024

Another reason we don't want to use a string is that we need to do in-place decoding on the raw data, which would better be done on a raw byte buffer. Although C++ strings are mutable, since the mutation may change the defacto size of the string, it would be hard for us to maintain a valid state of the string object. For example, we may end up getting a string whose .length() method returns a value greater than the actual length of the string. As far as I know, the security reviewers are fine with this approach if there is nothing else we can do.

@JasonYeMSFT JasonYeMSFT removed the ❔ investigate We need to look into this further label Jan 24, 2024
@JasonYeMSFT
Copy link
Contributor

Anyway, I managed to use string class to handle the memory management for us. Our code no longer has any usage of memcpy now.

@JasonYeMSFT JasonYeMSFT added the ✅ merged A fix for this issue has been merged label Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ merged A fix for this issue has been merged
Projects
None yet
Development

No branches or pull requests

3 participants