Repository navigation
Closed
Labels
Breaking-Changebreaking change that may affect usersIssue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Securitysecurity related areas such as JEA
Milestone
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
In 7.2.0-preview.9, ConvertTo-SecureString with either -Key or -SecureKey truncates the payload down to the nearest multiple of eight characters. ConvertFrom-SecureString seems to be fine; SecureStrings created in 7.1.4 and imported to 7.2.0-preview.9 aren't truncated when converted to plaintext.
Expected behavior
PS> $Key = [Byte[]]::New(32)
>> '[8Chars][8Chars][Not8]' |
>> ConvertTo-SecureString -AsPlainText -Force |
>> ConvertFrom-SecureString -Key $Key |
>> ConvertTo-SecureString -Key $Key |
>> ConvertFrom-SecureString -AsPlainText
[8Chars][8Chars][Not8]
PS> $Key = [Byte[]]::New(32)
>> '🟥🟧🟨🟩🟦🟪' |
>> ConvertTo-SecureString -AsPlainText -Force |
>> ConvertFrom-SecureString -Key $Key |
>> ConvertTo-SecureString -Key $Key |
>> ConvertFrom-SecureString -AsPlainText
🟥🟧🟨🟩🟦🟪Actual behavior
PS> $Key = [Byte[]]::New(32)
>> '[8Chars][8Chars][Not8]' |
>> ConvertTo-SecureString -AsPlainText -Force |
>> ConvertFrom-SecureString -Key $Key |
>> ConvertTo-SecureString -Key $Key |
>> ConvertFrom-SecureString -AsPlainText
[8Chars][8Chars]
PS> $Key = [Byte[]]::New(32)
>> '🟥🟧🟨🟩🟦🟪' |
>> ConvertTo-SecureString -AsPlainText -Force |
>> ConvertFrom-SecureString -Key $Key |
>> ConvertTo-SecureString -Key $Key |
>> ConvertFrom-SecureString -AsPlainText
🟥🟧🟨🟩Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.2.0-preview.9
PSEdition Core
GitCommitId 7.2.0-preview.9
OS Microsoft Windows 10.0.22449
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersIssue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Securitysecurity related areas such as JEA
Activity
dwtaber commentedon Sep 5, 2021
I'm a little out of my depth, but could this breaking change to System.Security.Cryptography.CryptoStream be the culprit?
fMichaleczek commentedon Sep 7, 2021
dwtaber out of my depth too, but i believe you're right.
There is a breaking change in net 6.0 Preview 6 inside the API System.Security.Cryptography.CryptoStream.Read
Breaking Change Documentation :
https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams
The API is used Line 372 :
PowerShell/src/System.Management.Automation/security/SecureStringHelper.cs
Lines 334 to 388 in 17986d8
vexx32 commentedon Sep 7, 2021
Yeah, looks like we'll need to add a loop in around that code to ensure we're reading all the data.
That's a very strange way to write a stream api, to me at least.
dwtaber commentedon Sep 7, 2021
The following change to the CryptoStream using statement starting at line 363 of SecureStringHelper.cs appears to work for me:
Are there any security implications or other glaring issues with the code above? If not, is there a better way to achieve the result? I'm new to collaborating on a project like this and don't want to create a pull request prematurely.
vexx32 commentedon Sep 7, 2021
I think rather than doing
.ToArray().AsSpan()for thetempDecryptedDatawe're probably better off just creating a new Span or array of the appropriate length instead, which is more similar to the original code. Otherwise I'd expect that'll end up reading the memory stream multiple times, which might break some things.Other than that, I think that would work? Would need to be tested of course, but that looks pretty good. You could probably skip the
forloop as well, because the decrypted data will already be intempDecryptedData, so that can end up as the final span, and you can call.ToArray()on that if you really need to.Looking at the docs for
CryptoStream.ReadI think you also need to specify the offset and count anyway, it doesn't look like its Read method has optional parameters: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptostream.read?view=net-5.0... Hm... The crypto stream API doesn't seem like it has a lot of span-friendly overloads 😕
That might cause some issues for how you want to solve this. Best bet is to test it and see if it compiles / behaves like you expect.
dwtaber commentedon Sep 7, 2021
Weird that it's not documented there, but it looks like
CryptoStreaminheritsStream.Read(Span<byte> buffer).(Edit: there's apparently also a new overload for
Array.Clearthat clears an entire array without requiring a start index and count to be specified, so I've made use of that as well.)You were right about the
forloop; it doesn't appear to be necessary. I think theToArray()at the end is necessary, asSecureStringHelper.Newwantsbyte[], and I don't think there's any implicit conversion fromSpan<T>toT[]. Here's a revised version of my possible fix. It does compile and works in cases similar to the ones shown in the issue description, for at least as many as 512 characters.There seem to be some extraneous bytes at the end of
tempDecryptedData; if the range isn't specified when converting tobyte[], theSecureStringreturned by the method winds up with a handful of extra bytes that, in the cases I tried, wound up getting interpreted as roughly three to five (seemingly) random non-Latin characters at the end of the intended string. The characters were different each time I ran the same set of commands; I'm not sure if that's because the random key was different each time, or maybe some other factor. Specifying the[0..totalBytesRead]range to be converted tobyte[]appears to resolve the issue, but I mention it in case it points to some potential security issue or edge case that I wouldn't know to look for.PaulHigin commentedon Sep 7, 2021
WG-Security
Thanks for finding this. I am just beginning to look into it and will propose a PR if no one else does.
dwtaber commentedon Sep 7, 2021
I'm going over the documentation re: contributing to make sure I get the PR right. I do have a potentially silly question that I haven't run into a specific answer for yet re: testing. Is it okay to modify an extant Pester test, or is it preferable to make a whole new one? \test\powershell\Modules\Microsoft.PowerShell.Security\SecureString.Tests.ps1 would have caught this bug with only a little modification, so making the necessary adjustments seems like the path of least resistance, but I'm happy to write a new test if that's the way things are done.
Apologies if the answer's somewhere I should've seen it, or if I shouldn't be asking here.
vexx32 commentedon Sep 7, 2021
As long as the test still serves its original purpose (assuming that purpose still makes sense, of course) then yeah that should usually be fine.
An explicit test might be preferable in this case, just because of the esoteric nature of the bug -- probably would be good to link to the breaking change doc that was mentioned here in a comment near the test as well.
PaulHigin commentedon Sep 7, 2021
I was thinking of using a different pattern.
Also, I noticed that the Encrypt() method doesn't dispose disposable objects or always clear the clear text data. So I feel it should be cleaned up too. I'll submit a PR tomorrow and let you take a look.
PaulHigin commentedon Sep 8, 2021
I have created a PR for this (see above). Please take a look.
dwtaber commentedon Sep 8, 2021
I'm no expert, but that definitely seems like a much cleaner and more straightforward approach. Parts of the original code seemed clunky enough that I figured they must be necessary for reasons I just wasn't seeing. I'm happy to learn I was wrong about that! Is there a performance reason to stick with
Array.Clear(Array, Int32, Int32)rather than use the newArray.Clear(Array)overload in thefinallyblock?Sorry, terminology misfire on my part; I wasn't thinking in terms of each
Itblock constituting a test. Anyway, yeah, my plan was to replace$string = "ABCD"in theBeforeAllblock with something long enough to reveal this issue, e.g.$String = "PrimeNumberOfCharacters", and to add thisItblock:Edit: just noticed that the PR already includes updated tests. Huzzah!
PaulHigin commentedon Sep 9, 2021
Yes, I believe I have covered all issues in the PR. Feel free to post any additional comments directly in the PR. I am closing this issue since it has been addressed.
Thanks for finding this and helping with the fix!