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

Fix mixed elevation integration issue by using MemoryStream #4413

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/AppInstallerCLICore/ConfigurationDynamicRuntimeFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,16 @@ namespace AppInstaller::CLI::ConfigurationRemoting
highIntegritySet.Serialize(memoryStream);

Streams::DataReader reader(memoryStream.GetInputStreamAt(0));
reader.UnicodeEncoding(Streams::UnicodeEncoding::Utf8);
reader.LoadAsync((uint32_t)memoryStream.Size());

winrt::hstring result;
uint32_t bytesToRead = reader.UnconsumedBufferLength();

if (bytesToRead > 0)
{
result = reader.ReadString(bytesToRead);
}

return winrt::to_string(result);
THROW_HR_IF(E_UNEXPECTED, memoryStream.Size() > std::numeric_limits<uint32_t>::max());
uint32_t streamSize = (uint32_t)memoryStream.Size();
std::vector<uint8_t> bytes;
bytes.resize(streamSize);
reader.LoadAsync(streamSize);
reader.ReadBytes(bytes);
reader.DetachStream();
memoryStream.Close();

return { bytes.begin(), bytes.end() };
}

ProcessorMap::iterator CreateSetProcessorForIntegrityLevel(Security::IntegrityLevel integrityLevel)
Expand Down
15 changes: 7 additions & 8 deletions src/ConfigurationRemotingServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text.Json.Serialization;
using Microsoft.Management.Configuration;
using Microsoft.Management.Configuration.Processor;
using Windows.Storage.Streams;
using WinRT;

namespace ConfigurationRemotingServer
Expand Down Expand Up @@ -118,14 +117,14 @@ static int Main(string[] args)

// Parse limitation set.
byte[] limitationSetBytes = Encoding.UTF8.GetBytes(commandStr.Substring(secondSeparatorIndex + CommandLineSectionSeparator.Length));
InMemoryRandomAccessStream limitationSetStream = new InMemoryRandomAccessStream();
DataWriter streamWriter = new DataWriter(limitationSetStream);
streamWriter.WriteBytes(limitationSetBytes);
streamWriter.StoreAsync().GetAwaiter().GetResult();
streamWriter.DetachStream();
limitationSetStream.Seek(0);
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(limitationSetBytes);
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
ConfigurationProcessor processor = new ConfigurationProcessor(factory);
var limitationSetResult = processor.OpenConfigurationSet(limitationSetStream);
var limitationSetResult = processor.OpenConfigurationSet(memoryStream.AsInputStream());
memoryStream.Close();

if (limitationSetResult.ResultCode != null)
{
throw limitationSetResult.ResultCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ namespace winrt::Microsoft::Management::Configuration::implementation
// This is done here to enable easy cancellation propagation to the stream reads.
uint32_t bufferSize = 1 << 20;
Windows::Storage::Streams::Buffer buffer(bufferSize);
Windows::Storage::Streams::InputStreamOptions readOptions =
Windows::Storage::Streams::InputStreamOptions::Partial | Windows::Storage::Streams::InputStreamOptions::ReadAhead;

// Memory stream in mixed elevation does not support InputStreamOptions as flags.
Windows::Storage::Streams::InputStreamOptions readOptions = Windows::Storage::Streams::InputStreamOptions::Partial;
std::string inputString;

for (;;)
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Management.Configuration/ConfigurationSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ namespace winrt::Microsoft::Management::Configuration::implementation
{
std::unique_ptr<ConfigurationSetSerializer> serializer = ConfigurationSetSerializer::CreateSerializer(m_schemaVersion);
hstring result = serializer->Serialize(this);
auto resultUtf8 = winrt::to_string(result);
std::vector<uint8_t> bytes(resultUtf8.begin(), resultUtf8.end());

Windows::Storage::Streams::DataWriter dataWriter{ stream };
dataWriter.UnicodeEncoding(Windows::Storage::Streams::UnicodeEncoding::Utf8);
dataWriter.WriteString(result);
dataWriter.WriteBytes(bytes);
dataWriter.StoreAsync().get();
dataWriter.DetachStream();
}
Expand Down
Loading