How can I implement ISerializable in .NET 4+ without violating inheritance security rules?

前端 未结 3 1106
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:29

Background: Noda Time contains many serializable structs. While I dislike binary serialization, we received many requests to support it, back in the 1.x timeline. We support it

相关标签:
3条回答
  • 2021-01-30 19:39

    The accepted answer is so convincing that I almost believed this wasn't a bug. But after doing some experiments now I can say that Level2 security is a complete mess; at least, something is really fishy.

    A couple of days ago I bumped into the same issue with my libraries. I quickly created a unit test; however, I could not reproduce the problem I experienced in .NET Fiddle, while the very same code "successfully" threw the exception in a console app. In the end I found two weird ways to overcome the issue.

    TL;DR: It turns out that if you use an internal type of the used library in your consumer project, then the partially trusted code works as expected: it is able to instantiate an ISerializable implementation (and a security critical code cannot be called directly, but see below). Or, which is even more ridiculous, you can try to create the sandbox again if it didn't work for the first time...

    But let's see some code.

    ClassLibrary.dll:

    Let's separate two cases: one for a regular class with security critical content and one ISerializable implementation:

    public class CriticalClass
    {
        public void SafeCode() { }
    
        [SecurityCritical]
        public void CriticalCode() { }
    
        [SecuritySafeCritical]
        public void SafeEntryForCriticalCode() => CriticalCode();
    }
    
    [Serializable]
    public class SerializableCriticalClass : CriticalClass, ISerializable
    {
        public SerializableCriticalClass() { }
    
        private SerializableCriticalClass(SerializationInfo info, StreamingContext context) { }
    
        [SecurityCritical]
        public void GetObjectData(SerializationInfo info, StreamingContext context) { }
    }
    

    One way to overcome the issue is to use an internal type from the consumer assembly. Any type will do it; now I define an attribute:

    [AttributeUsage(AttributeTargets.All)]
    internal class InternalTypeReferenceAttribute : Attribute
    {
        public InternalTypeReferenceAttribute() { }
    }
    

    And the relevant attributes applied to the assembly:

    [assembly: InternalsVisibleTo("UnitTest, PublicKey=<your public key>")]
    [assembly: AllowPartiallyTrustedCallers]
    [assembly: SecurityRules(SecurityRuleSet.Level2, SkipVerificationInFullTrust = true)]
    

    Sign the assembly, apply the key to the InternalsVisibleTo attribute and prepare for test project:

    UnitTest.dll (uses NUnit and ClassLibrary):

    To use the internal trick the test assembly should be signed as well. Assembly attributes:

    // Just to make the tests security transparent by default. This helps to test the full trust behavior.
    [assembly: AllowPartiallyTrustedCallers] 
    
    // !!! Comment this line out and the partial trust test cases may fail for the fist time !!!
    [assembly: InternalTypeReference]
    

    Note: The attribute can be applied anywhere. In my case it was on a method in a random test class took me a couple of days to find.

    Note 2: If you run all test methods together it can happen that the tests will pass.

    The skeleton of the test class:

    [TestFixture]
    public class SecurityCriticalAccessTest
    {
        private partial class Sandbox : MarshalByRefObject
        {
        }
    
        private static AppDomain CreateSandboxDomain(params IPermission[] permissions)
        {
            var evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
            var permissionSet = GetPermissionSet(permissions);
            var setup = new AppDomainSetup
            {
                ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
            };
    
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            var strongNames = new List<StrongName>();
            foreach (Assembly asm in assemblies)
            {
                AssemblyName asmName = asm.GetName();
                strongNames.Add(new StrongName(new StrongNamePublicKeyBlob(asmName.GetPublicKey()), asmName.Name, asmName.Version));
            }
    
            return AppDomain.CreateDomain("SandboxDomain", evidence, setup, permissionSet, strongNames.ToArray());
        }
    
        private static PermissionSet GetPermissionSet(IPermission[] permissions)
        {
            var evidence = new Evidence();
            evidence.AddHostEvidence(new Zone(SecurityZone.Internet));
            var result = SecurityManager.GetStandardSandbox(evidence);
            foreach (var permission in permissions)
                result.AddPermission(permission);
            return result;
        }
    }
    

    And let's see the test cases one by one

    Case 1: ISerializable implementation

    The same issue as in the question. The test passes if

    • InternalTypeReferenceAttribute is applied
    • sandbox is tried to be created multiple times (see the code)
    • or, if all the test cases are executed at once and this is not the first one

    Otherwise, there comes the totally inappropriate Inheritance security rules violated while overriding member... exception when you instantiate SerializableCriticalClass.

    [Test]
    [SecuritySafeCritical] // for Activator.CreateInstance
    public void SerializableCriticalClass_PartialTrustAccess()
    {
        var domain = CreateSandboxDomain(
            new SecurityPermission(SecurityPermissionFlag.SerializationFormatter), // BinaryFormatter
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); // Assert.IsFalse
        var handle = Activator.CreateInstance(domain, Assembly.GetExecutingAssembly().FullName, typeof(Sandbox).FullName);
        var sandbox = (Sandbox)handle.Unwrap();
        try
        {
            sandbox.TestSerializableCriticalClass();
            return;
        }
        catch (Exception e)
        {
            // without [InternalTypeReference] it may fail for the first time
            Console.WriteLine($"1st try failed: {e.Message}");
        }
    
        domain = CreateSandboxDomain(
            new SecurityPermission(SecurityPermissionFlag.SerializationFormatter), // BinaryFormatter
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); // Assert.IsFalse
        handle = Activator.CreateInstance(domain, Assembly.GetExecutingAssembly().FullName, typeof(Sandbox).FullName);
        sandbox = (Sandbox)handle.Unwrap();
        sandbox.TestSerializableCriticalClass();
    
        Assert.Inconclusive("Meh... succeeded only for the 2nd try");
    }
    
    private partial class Sandbox
    {
        public void TestSerializableCriticalClass()
        {
            Assert.IsFalse(AppDomain.CurrentDomain.IsFullyTrusted);
    
            // ISerializable implementer can be created.
            // !!! May fail for the first try if the test does not use any internal type of the library. !!!
            var critical = new SerializableCriticalClass();
    
            // Critical method can be called via a safe method
            critical.SafeEntryForCriticalCode();
    
            // Critical method cannot be called directly by a transparent method
            Assert.Throws<MethodAccessException>(() => critical.CriticalCode());
            Assert.Throws<MethodAccessException>(() => critical.GetObjectData(null, new StreamingContext()));
    
            // BinaryFormatter calls the critical method via a safe route (SerializationFormatter permission is required, though)
            new BinaryFormatter().Serialize(new MemoryStream(), critical);
        }
    
    }
    

    Case 2: Regular class with security critical members

    The test passes under the same conditions as the first one. However, the issue is completely different here: a partially trusted code may access a security critical member directly.

    [Test]
    [SecuritySafeCritical] // for Activator.CreateInstance
    public void CriticalClass_PartialTrustAccess()
    {
        var domain = CreateSandboxDomain(
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess), // Assert.IsFalse
            new EnvironmentPermission(PermissionState.Unrestricted)); // Assert.Throws (if fails)
        var handle = Activator.CreateInstance(domain, Assembly.GetExecutingAssembly().FullName, typeof(Sandbox).FullName);
        var sandbox = (Sandbox)handle.Unwrap();
        try
        {
            sandbox.TestCriticalClass();
            return;
        }
        catch (Exception e)
        {
            // without [InternalTypeReference] it may fail for the first time
            Console.WriteLine($"1st try failed: {e.Message}");
        }
    
        domain = CreateSandboxDomain(
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess)); // Assert.IsFalse
        handle = Activator.CreateInstance(domain, Assembly.GetExecutingAssembly().FullName, typeof(Sandbox).FullName);
        sandbox = (Sandbox)handle.Unwrap();
        sandbox.TestCriticalClass();
    
        Assert.Inconclusive("Meh... succeeded only for the 2nd try");
    }
    
    private partial class Sandbox
    {
        public void TestCriticalClass()
        {
            Assert.IsFalse(AppDomain.CurrentDomain.IsFullyTrusted);
    
            // A type containing critical methods can be created
            var critical = new CriticalClass();
    
            // Critical method can be called via a safe method
            critical.SafeEntryForCriticalCode();
    
            // Critical method cannot be called directly by a transparent method
            // !!! May fail for the first time if the test does not use any internal type of the library. !!!
            // !!! Meaning, a partially trusted code has more right than a fully trusted one and is       !!!
            // !!! able to call security critical method directly.                                        !!!
            Assert.Throws<MethodAccessException>(() => critical.CriticalCode());
        }
    }
    

    Case 3-4: Full trust versions of case 1-2

    For the sake of completeness here are the same cases as the ones above executed in a fully trusted domain. If you remove [assembly: AllowPartiallyTrustedCallers] the tests fail because then you can access critical code directly (as the methods are not transparent by default anymore).

    [Test]
    public void CriticalClass_FullTrustAccess()
    {
        Assert.IsTrue(AppDomain.CurrentDomain.IsFullyTrusted);
    
        // A type containing critical methods can be created
        var critical = new CriticalClass();
    
        // Critical method cannot be called directly by a transparent method
        Assert.Throws<MethodAccessException>(() => critical.CriticalCode());
    
        // Critical method can be called via a safe method
        critical.SafeEntryForCriticalCode();
    }
    
    [Test]
    public void SerializableCriticalClass_FullTrustAccess()
    {
        Assert.IsTrue(AppDomain.CurrentDomain.IsFullyTrusted);
    
        // ISerializable implementer can be created
        var critical = new SerializableCriticalClass();
    
        // Critical method cannot be called directly by a transparent method (see also AllowPartiallyTrustedCallersAttribute)
        Assert.Throws<MethodAccessException>(() => critical.CriticalCode());
        Assert.Throws<MethodAccessException>(() => critical.GetObjectData(null, default(StreamingContext)));
    
        // Critical method can be called via a safe method
        critical.SafeEntryForCriticalCode();
    
        // BinaryFormatter calls the critical method via a safe route
        new BinaryFormatter().Serialize(new MemoryStream(), critical);
    }
    

    Epilogue:

    Of course, this will not solve your problem with .NET Fiddle. But now I would be very surprised if it wasn't a bug in the framework.

    The biggest question to me now is the quoted part in the accepted answer. How did they come out with this nonsense? The ISafeSerializationData is clearly not a solution for anything: it is used exclusively by the base Exception class and if you subscribe the SerializeObjectState event (why isn't that an overridable method?), then the state will also be consumed by the Exception.GetObjectData in the end.

    The AllowPartiallyTrustedCallers/SecurityCritical/SecuritySafeCritical triumvirate of attributes were designed for exactly the usage shown above. It seems total nonsense to me that a partially trusted code cannot even instantiate a type regardless of the attempt using its security critical members. But it is an even bigger nonsense (a security hole actually) that a partially trusted code may access a security critical method directly (see case 2) whereas this is forbidden for transparent methods even from a fully trusted domain.

    So if your consumer project is a test or another well-known assembly, then the internal trick can be used perfectly. For .NET Fiddle and other real-life sandboxed environments the only solution is reverting back to SecurityRuleSet.Level1 until this is fixed by Microsoft.


    Update: A Developer Community ticket has been created for the issue.

    0 讨论(0)
  • 2021-01-30 19:48

    According to the MSDN see:

    How to Fix Violations?

    To fix a violation of this rule, make the GetObjectData method visible and overridable and make sure all instance fields are included in the serialization process or explicitly marked with the NonSerializedAttribute attribute.

    The following example fixes the two previous violations by providing an overrideable implementation of ISerializable.GetObjectData on the Book class and by providing an implementation of ISerializable.GetObjectData on the Library class.

    using System;
    using System.Security.Permissions;
    using System.Runtime.Serialization;
    
    namespace Samples2
    {
        [Serializable]
        public class Book : ISerializable
        {
            private readonly string _Title;
    
            public Book(string title)
            {
                if (title == null)
                    throw new ArgumentNullException("title");
    
                _Title = title;
            }
    
            protected Book(SerializationInfo info, StreamingContext context)
            {
                if (info == null)
                    throw new ArgumentNullException("info");
    
                _Title = info.GetString("Title");
            }
    
            public string Title
            {
                get { return _Title; }
            }
    
            [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
            protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue("Title", _Title);
            }
    
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
            void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
            {
                if (info == null)
                    throw new ArgumentNullException("info");
    
                GetObjectData(info, context);
            }
        }
    
        [Serializable]
        public class LibraryBook : Book
        {
            private readonly DateTime _CheckedOut;
    
            public LibraryBook(string title, DateTime checkedOut)
                : base(title)
            {
                _CheckedOut = checkedOut;
            }
    
            protected LibraryBook(SerializationInfo info, StreamingContext context)
                : base(info, context)
            {
                _CheckedOut = info.GetDateTime("CheckedOut");
            }
    
            public DateTime CheckedOut
            {
                get { return _CheckedOut; }
            }
    
            [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
            protected override void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                base.GetObjectData(info, context);
    
                info.AddValue("CheckedOut", _CheckedOut);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 19:49

    According to the MSDN, in .NET 4.0 basically you should not use ISerializable for partially trusted code, and instead you should use ISafeSerializationData

    Quoting from https://docs.microsoft.com/en-us/dotnet/standard/serialization/custom-serialization

    Important

    In versions previous to .NET Framework 4.0, serialization of custom user data in a partially trusted assembly was accomplished using the GetObjectData. Starting with version 4.0, that method is marked with the SecurityCriticalAttribute attribute which prevents execution in partially trusted assemblies. To work around this condition, implement the ISafeSerializationData interface.

    So probably not what you wanted to hear if you need it, but I don't think there's any way around it while keeping using ISerializable (other than going back to Level1 security, which you said you don't want to).

    PS: the ISafeSerializationData docs state that it is just for exceptions, but it doesn't seem all that specific, you may want to give it a shot... I basically can't test it with your sample code (other than removing ISerializable works, but you knew that already)... you'll have to see if ISafeSerializationData suits you enough.

    PS2: the SecurityCritical attribute doesn't work because it's ignored when the assembly is loaded in partial trust mode (on Level2 security). You can see it on your sample code, if you debug the target variable in ExecuteUntrustedCode right before invoking it, it'll have IsSecurityTransparent to true and IsSecurityCritical to false even if you mark the method with the SecurityCritical attribute)

    0 讨论(0)
提交回复
热议问题