Unable to allocate the free space to main volume
The command diskutil cs resizeStack ... requires some unallocated disk space at the end of a Logical Volume Group and the Recovery HD (index 2 &amp; 3) to be able to expand it. You only have 1800 blocks à 512 Bytes (~2 MB) of it.

After installing and uninstalling Ubuntu you still have an abandoned BIOS Boot partition (probably GRUB) (index 4), a swap partition (index 5) and a main Linux partition (index 6) at the tail of your disk.

Before resizing the CS stack (which often fails with such an expelled Linux dual-boot environment), you have to delete the three Linux partitions (and change the MBR to the default pMBR).


Backup your internal drive
Detach any external drive
Boot to Internet Recovery Mode
Open Terminal in the menubar Utilities -> Terminal
Get an overview (especially the gpt command is important!). Below I assume the internal disk has the disk identifier disk0 and the Logical Volume residing in disk0s2 has the disk identifier disk2. Please use the disk identifiers you found in your environment:

diskutil list
gpt -r show disk0

Unmount first the Logical VOlume and then the internal disk:

diskutil umountDisk /dev/disk2
diskutil umountDisk /dev/disk0

Delete the MBR:

dd if=/dev/zero of=/dev/disk0 bs=512 count=1

Destroy the GUID partition table and create a new one (this also creates a fresh pMBR):

gpt destroy disk0
gpt create -f disk0

Rebuild all previous GUID partitions:

gpt add -i 1 -b 40 -s 409600 -t C12A7328-F81F-11D2-BA4B-00A0C93EC93B disk0
gpt add -i 3 -b 840002008 -s 1269536 -t 426F6F74-0000-11AA-AA11-00306543ECAC disk0
gpt add -i 2 -b 409640 -s 839592368 -t 53746F72-6167-11AA-AA11-00306543ECAC disk0


If you get a resource busy error after one of the steps, just unmount disk0 again with 

diskutil umountDisk /dev/disk0



Check the disk with diskutil verifyDisk disk0 afterwards. 

Enter diskutil cs list and check if all four CoreStorage containers appear: a Logical Volume Group, a Physical Volume and Logical Volume Family and a Logical Volume.

With the UUID of the Logical Volume mount the LV:

Example:

    +-&gt; Logical Volume FCF7976D-78ED-4CE9-8029-C6557CB601C5
        ---------------------------------------------------
        Disk:                  disk17
        Status:                Online


Then use:

diskutil mount FCF7976D-78ED-4CE9-8029-C6557CB601C5


Then after getting the disk identifier of the mounted LV with diskutil list verify the volume:

diskutil verifyVolume disk17 #probably it's disk17, disk16 or disk18


Then try to resize the stack with:

diskutil cs resizeStack FCF7976D-78ED-4CE9-8029-C6557CB601C5 499g


If you get an error (partition is too small) use a slightly smaller value (e.g. 498500m)



The above resizeStack command may fail, due to a CS LVG "corruption". This is no corruption of your data but probably a corruption/misconfiguration of some CS meta data. Then you have to wipe your disk and restore your current (Time Machine) backup.