- extern int print_map_addr;
-#endif /* 0 */
-
- /*
- * Find the inode; verify that it's a regular file.
- */
-
- err = fdgetf(p, fd, &fp);
- if (err)
- return(err);
-
- if (fp->f_type != DTYPE_VNODE)
- return(KERN_INVALID_ARGUMENT);
- vp = (struct vnode *)fp->f_data;
-
- if (vp->v_type != VREG)
- return (KERN_INVALID_ARGUMENT);
-
- if (offset & PAGE_MASK_64) {
- printf("map_fd: file offset not page aligned(%d : %s\)n",p->p_pid, p->p_comm);
- return (KERN_INVALID_ARGUMENT);
- }
- map_size = round_page(size);
-
- /*
- * Allow user to map in a zero length file.
- */
- if (size == 0)
- return (KERN_SUCCESS);
- /*
- * Map in the file.
- */
- UBCINFOCHECK("map_fd_funneled", vp);
- pager = (void *) ubc_getpager(vp);
- if (pager == NULL)
- return (KERN_FAILURE);
-
-
- my_map = current_map();
-
- result = vm_map_64(
- my_map,
- &map_addr, map_size, (vm_offset_t)0, TRUE,
- pager, offset, TRUE,
- VM_PROT_DEFAULT, VM_PROT_ALL,
- VM_INHERIT_DEFAULT);
- if (result != KERN_SUCCESS)
- return (result);
-
-
- if (!findspace) {
- vm_offset_t dst_addr;
- vm_map_copy_t tmp;
-
- if (copyin(va, &dst_addr, sizeof (dst_addr)) ||
- trunc_page(dst_addr) != dst_addr) {
- (void) vm_map_remove(
- my_map,
- map_addr, map_addr + map_size,
- VM_MAP_NO_FLAGS);
- return (KERN_INVALID_ADDRESS);
- }
-
- result = vm_map_copyin(
- my_map,
- map_addr, map_size, TRUE,
- &tmp);
- if (result != KERN_SUCCESS) {
-
- (void) vm_map_remove(
- my_map,
- map_addr, map_addr + map_size,
- VM_MAP_NO_FLAGS);
- return (result);
- }
-
- result = vm_map_copy_overwrite(
- my_map,
- dst_addr, tmp, FALSE);
- if (result != KERN_SUCCESS) {
- vm_map_copy_discard(tmp);
- return (result);
- }
- } else {
- if (copyout(&map_addr, va, sizeof (map_addr))) {
- (void) vm_map_remove(
- my_map,
- map_addr, map_addr + map_size,
- VM_MAP_NO_FLAGS);
- return (KERN_INVALID_ADDRESS);
- }
- }
-
- ubc_setcred(vp, current_proc());
- ubc_map(vp);
-
- return (KERN_SUCCESS);
+ kprintf("%s vpath %s cryptid 0x%08x cputype 0x%08x cpusubtype 0x%08x range 0x%016llx size 0x%016llx\n",
+ __FUNCTION__, vpath, cryptid, cputype, cpusubtype, (uint64_t)user_addr, (uint64_t)user_size);
+#endif
+
+ /* set up decrypter first */
+ crypt_file_data_t crypt_data = {
+ .filename = vpath,
+ .cputype = cputype,
+ .cpusubtype = cpusubtype };
+ result = text_crypter_create(&crypt_info, cryptname, (void*)&crypt_data);
+ FREE_ZONE(vpath, MAXPATHLEN, M_NAMEI);
+
+ if(result) {
+ printf("%s: unable to create decrypter %s, kr=%d\n",
+ __FUNCTION__, cryptname, result);
+ if (result == kIOReturnNotPrivileged) {
+ /* text encryption returned decryption failure */
+ return (EPERM);
+ } else {
+ return (ENOMEM);
+ }
+ }
+
+ /* now remap using the decrypter */
+ result = vm_map_apple_protected(user_map, user_addr, user_addr+user_size, &crypt_info);
+ if (result) {
+ printf("%s: mapping failed with %d\n", __FUNCTION__, result);
+ crypt_info.crypt_end(crypt_info.crypt_ops);
+ return (EPERM);
+ }
+
+ return 0;