In the UK, who owns the intellectual property of a thesis?
Depends on the University. The university (in the UK) I most recently worked at had an Intellectual Property policy that all IP rights for students stayed with the student automatically, which differed from the policy for research staff. But the university would be happy to get involved to help commercialise in return for a share, much as your suggestion about an agreement. However, other universities have other IP policies. But whatever their policy is, it should be easily available from their intranet though it might not be available to people outside the institution.