Trusting .NET Code

Developing code with .NET is hard enough without worrying about security. This means that most developers take the easy way out and assume that their code will execute in a fully trusted environment. Full trust means that the user implicitly trust the code not to do anything nasty, and allows it full access to the resources on a machine or network. This includes giving it access to risky classes in the framework, such as those that read and write disk files, and-gasp!-unmanaged code.

From a developer's standpoint, assuming that code has full trust saves the tedious work of handling security exceptions every time they use an assembly from the framework or other source. Simple, and easier to meet insane deadlines imposed by clients and bosses.

But .NET also allows for partially trusted code, sometimes called semi-trusted in the .NET literature. With partially trusted code, the user can restrict what the code can do. For example, code that calculates some obscure statistical regression probably doesn't need to access any files on disk, assuming it already has the data it needs in memory. The user trusts it enough to let it run, but not enough to let it have free reign on all system resources. This is consistent with the security principle of giving code and users just the permissions they need to do their job, but no more. So a partially trusted application is restricted in some way by the framework.

The key to partially trusted code is the AllowPartiallyTrustedCallersAttribute in the System.Security namespace, commonly abbreviated APTCA. APTCA is an attribute used to mark assemblies, and does not take any arguments. The presences of this attribute serve to limit how attacks can be made through the code. Any managed code with a strong name is unusable by partially trusted code unless the code is marked with APTCA.

Here is where the work begins. If your application uses a strongly named assembly unmarked with APTCA, and partially trusted code calls your assembly, the call will fail if any downstream assembly is strongly named but does not have APTCA itself. This is all part of the security structure in .NET where all code must have a minimum level of access permissions before .NET will allow it to run. Assemblies in the .NET framework are no exception to this requirement, so you have to be sure that any system assemblies you use have the attribute as well. In the list of references below, I've included a line to an MSDN article that lists the System assemblies with APTCA.

Probably the only reliable way of determining whether a given assembly has APTCA set is to use the .NET framework SDK tool, ildasm.exe. Fire it up either from the command line or from the Start|Run menu in Windows, and load the assembly you're interested in (most system DLLs are located in C:\WINNT\Microsoft.NET\Framework\[framework version]). One you've loaded the assembly, double-click the MANIFEST item near the top of the treeview, and search through the manifest for APTCA.

Join the PC World newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Don Kiely

Computerworld
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?