Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. FileIOPermission also takes away UIPermission?

FileIOPermission also takes away UIPermission?

Scheduled Pinned Locked Moved C#
securityquestionannouncement
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nathan Blomquist
    wrote on last edited by
    #1

    I was trying to limit some permissions on what certain methods can and cannot access. I wanted to disallow certain methods from accessing files. I found the FileIOPermissionAttribute, this seemed exactly what I wanted. But alas, if I use this attribute I cannot call showdialog on a form object.[FileIOPermission(SecurityAction.PermitOnly)] private void button1_Click(object sender, System.EventArgs e) { Form2 fm = new Form2(); fm.ShowDialog(); }
    The exception text: An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll Additional information: Request for the permission of type System.Security.Permissions.UIPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. Can anyone explain why a UIPermission exception is thrown? -Nathan --------------------------- Hmmm... what's a signature?

    H 1 Reply Last reply
    0
    • N Nathan Blomquist

      I was trying to limit some permissions on what certain methods can and cannot access. I wanted to disallow certain methods from accessing files. I found the FileIOPermissionAttribute, this seemed exactly what I wanted. But alas, if I use this attribute I cannot call showdialog on a form object.[FileIOPermission(SecurityAction.PermitOnly)] private void button1_Click(object sender, System.EventArgs e) { Form2 fm = new Form2(); fm.ShowDialog(); }
      The exception text: An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll Additional information: Request for the permission of type System.Security.Permissions.UIPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. Can anyone explain why a UIPermission exception is thrown? -Nathan --------------------------- Hmmm... what's a signature?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Because you're granting permissions ONLY for the FileIOPermission, not the UIPermission which Form.ShowDialog requires. If you're new to .NET code access security, "UB" has an excellent article here on CodeProject, Understanding .NET Code Access Security[^]. You should also see the reference, Code Access Security[^], in the .NET Framework SDK.

      Microsoft MVP, Visual C# My Articles

      N 1 Reply Last reply
      0
      • H Heath Stewart

        Because you're granting permissions ONLY for the FileIOPermission, not the UIPermission which Form.ShowDialog requires. If you're new to .NET code access security, "UB" has an excellent article here on CodeProject, Understanding .NET Code Access Security[^]. You should also see the reference, Code Access Security[^], in the .NET Framework SDK.

        Microsoft MVP, Visual C# My Articles

        N Offline
        N Offline
        Nathan Blomquist
        wrote on last edited by
        #3

        Is there and easy way to deny access to all files, but still allow UI to be presented to the user? Basically I am helping to develop a plugin architecture and we need to disable file access and possibly even socket access. So I was looking for something along the lines of: // psuedo code filePerm.DenyAllAccess(); sockPerm.DenyAllAccess(); myPlugin.DoSomethingClever(); sockPerm.RevertDeny(); filePerm.RevertDeny(); `Thanks, Nathan --------------------------- Hmmm... what's a signature?`

        H 1 Reply Last reply
        0
        • N Nathan Blomquist

          Is there and easy way to deny access to all files, but still allow UI to be presented to the user? Basically I am helping to develop a plugin architecture and we need to disable file access and possibly even socket access. So I was looking for something along the lines of: // psuedo code filePerm.DenyAllAccess(); sockPerm.DenyAllAccess(); myPlugin.DoSomethingClever(); sockPerm.RevertDeny(); filePerm.RevertDeny(); `Thanks, Nathan --------------------------- Hmmm... what's a signature?`

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Typically, it's not your code's responsibility. That's what permissions sets that code groups use are for. I don't see why you're trying to do this like you are. If a plugin calls your method, your method was written by "you" (possibly an org.) so you ultimately control what goes on in there. If you don't use files or sockets, how is a plugin supposed to change that? Now, if you wanted to make sure that your application had certain permissions but plugins were more restricted, you should consider using a code group based on the strong name of your assembl(y|ies). With real-world applications, you should always sign your assemblies for added benefit, like the ability to be installed into the GAC, to be used by other strong-name assemblies, etc. Use the AssemblyKeyNameAttribute or the AssemblyKeyFileAttribute with a file generated by "sn.exe -k KeyFile.snk", which generates a key pair. Use this for all of your assemblies (putting it in a container with "sn.exe -i MyContainerName KeyFile.snk" and using the AssemblyKeyNameAttribute is often easier with large projects). If you call one of your plugin methods as you do above, you have a number of things to consider. First, do you even trust the plugin? Perhaps checking the public key token and making sure it's the same as yours or another trusted source would be a good idea. If you want anyone to be able to extend your application, then you're on the right track. Create an instance of the permission(s) and call Deny on the instance. This also means that if callers higher in the stack are trying to access a resource, they won't be able to but a SecurityException will be thrown by callees lower in the stack when a Demand for that permission occurs.

          Microsoft MVP, Visual C# My Articles

          N 1 Reply Last reply
          0
          • H Heath Stewart

            Typically, it's not your code's responsibility. That's what permissions sets that code groups use are for. I don't see why you're trying to do this like you are. If a plugin calls your method, your method was written by "you" (possibly an org.) so you ultimately control what goes on in there. If you don't use files or sockets, how is a plugin supposed to change that? Now, if you wanted to make sure that your application had certain permissions but plugins were more restricted, you should consider using a code group based on the strong name of your assembl(y|ies). With real-world applications, you should always sign your assemblies for added benefit, like the ability to be installed into the GAC, to be used by other strong-name assemblies, etc. Use the AssemblyKeyNameAttribute or the AssemblyKeyFileAttribute with a file generated by "sn.exe -k KeyFile.snk", which generates a key pair. Use this for all of your assemblies (putting it in a container with "sn.exe -i MyContainerName KeyFile.snk" and using the AssemblyKeyNameAttribute is often easier with large projects). If you call one of your plugin methods as you do above, you have a number of things to consider. First, do you even trust the plugin? Perhaps checking the public key token and making sure it's the same as yours or another trusted source would be a good idea. If you want anyone to be able to extend your application, then you're on the right track. Create an instance of the permission(s) and call Deny on the instance. This also means that if callers higher in the stack are trying to access a resource, they won't be able to but a SecurityException will be thrown by callees lower in the stack when a Demand for that permission occurs.

            Microsoft MVP, Visual C# My Articles

            N Offline
            N Offline
            Nathan Blomquist
            wrote on last edited by
            #5

            Heath Stewart wrote: If you want anyone to be able to extend your application, then you're on the right track. Create an instance of the permission(s) and call Deny on the instance. I would like others to develop plugins so that is why I wanted to deny things around the method calls into that plugin. I don't want plugins to be able to do anything evil and such to the user's computer or via the user's computer. The plugin should get all information from the program and that is it. I have found this to work around the method declaration of the abstract plugin methods. abstract class plugin { // override this to add functionality protected abstract void OnDoSomething(); [FileIOPermission(SecurityAccess.Deny,Unrestricted=true)] public void DoSomething() { OnDoSomething(); } } This will stop file accessing. But I found that the plugin derived from this can just do an FileIOPermission.Assert to get around this. Maybe I am thinking too much and there is no truly safe way to do these plugins. Maybe at some point I have to trust my users... Thanks again, Nathan --------------------------- Hmmm... what's a signature?

            H 1 Reply Last reply
            0
            • N Nathan Blomquist

              Heath Stewart wrote: If you want anyone to be able to extend your application, then you're on the right track. Create an instance of the permission(s) and call Deny on the instance. I would like others to develop plugins so that is why I wanted to deny things around the method calls into that plugin. I don't want plugins to be able to do anything evil and such to the user's computer or via the user's computer. The plugin should get all information from the program and that is it. I have found this to work around the method declaration of the abstract plugin methods. abstract class plugin { // override this to add functionality protected abstract void OnDoSomething(); [FileIOPermission(SecurityAccess.Deny,Unrestricted=true)] public void DoSomething() { OnDoSomething(); } } This will stop file accessing. But I found that the plugin derived from this can just do an FileIOPermission.Assert to get around this. Maybe I am thinking too much and there is no truly safe way to do these plugins. Maybe at some point I have to trust my users... Thanks again, Nathan --------------------------- Hmmm... what's a signature?

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              The assert only works if the code that is asserting that permission was granted that permission (not taking callers higher in the stack into account). Again, this comes down to effective management of permission sets and code groups. That's the only true way to limit it.

              Microsoft MVP, Visual C# My Articles

              N 1 Reply Last reply
              0
              • H Heath Stewart

                The assert only works if the code that is asserting that permission was granted that permission (not taking callers higher in the stack into account). Again, this comes down to effective management of permission sets and code groups. That's the only true way to limit it.

                Microsoft MVP, Visual C# My Articles

                N Offline
                N Offline
                Nathan Blomquist
                wrote on last edited by
                #7

                Thanks for all your help. I will just have to deal with them having the ability to Assert to get around it. I think it will stop most people from doing anything stupid... but it won't stop true hackers. Thanks again, Nathan --------------------------- Hmmm... what's a signature?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups