[Mono-list] Problem with Syscall (or how to bind port 443 as a non-privileged user)

Edward Ned Harvey (mono) edward.harvey.mono at clevertrove.com
Mon Mar 3 02:11:29 UTC 2014


I have a service that needs to bind to port 443, and as far as I can tell, the best way to do this is to start as root (or sudo) and bind 443, and then lower privileges by using Mono.Unix.Native.Syscall.setuid.  Unfortunately my actual service product was failing to do this, so I created a *really* simple test project, and it still fails.

If there's a better way to solve this problem, I'd love to know.   ;-)  I am not married to Syscall.setuid.

If this is indicative of a problem with my mono build, I'll happily go look into that.  My first suspicion is that I'm just stupidly using it wrong somehow, or I'm barking up the wrong tree or something.

First things first:  I have mono 3.2.3 built freshly from source, on a centos 6 machine.  The whole configure/make/install process went without a problem.

I created a project, with reference to Mono.Posix.  Built it.  Copied the exe to /tmp.  Here is the result I get:

[root at centosbox ~]# cd /tmp
[root at centosbox tmp]# /usr/local/mono/bin/mono FunWithSyscall.exe 

Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for Mono.Unix.Native.Syscall ---> System.DllNotFoundException: libMonoPosixHelper.so
  at (wrapper managed-to-native) Mono.Unix.Native.Syscall:get_at_fdcwd ()
  at Mono.Unix.Native.Syscall..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at FunWithSyscall.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for Mono.Unix.Native.Syscall ---> System.DllNotFoundException: libMonoPosixHelper.so
  at (wrapper managed-to-native) Mono.Unix.Native.Syscall:get_at_fdcwd ()
  at Mono.Unix.Native.Syscall..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at FunWithSyscall.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0


Here is the complete source code of Program.cs:

using System;
using Mono.Posix;
using System.IO;
using System.Text;

namespace FunWithSyscall
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			byte[] buf;
			FileStream before = new FileStream ("before.txt", System.IO.FileMode.Create, FileAccess.Write, FileShare.None);
			buf = Encoding.UTF8.GetBytes ("Hello Before!");
			before.Write (buf, 0, buf.Length);
			before.Close ();

			Mono.Unix.Native.Syscall.setuid (498);
			Mono.Unix.Native.Syscall.setgid (498);

			FileStream after = new FileStream ("after.txt", System.IO.FileMode.Create, FileAccess.Write, FileShare.None);
			buf = Encoding.UTF8.GetBytes ("Hello After!");
			before.Write (buf, 0, buf.Length);
			before.Close ();
		}
	}
}


More information about the Mono-list mailing list