imap: fix out-of-range panic for imap updates

Check slice bounds before using it for the message and expunge updates.
Log the error but ignore the affected updates.

Link: https://lists.sr.ht/~rjarry/aerc-devel/%3CCJEHBFFUI11T.1AYGOMVGZ87ZS%40rek2system%3E
Reported-by: ReK2 <rek2@hispagatos.org>
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-04-28 21:51:54 +02:00 committed by Robin Jarry
parent 2f2a520ab0
commit 8f976af17b

View file

@ -281,6 +281,12 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
func (w *IMAPWorker) handleImapUpdate(update client.Update) { func (w *IMAPWorker) handleImapUpdate(update client.Update) {
w.worker.Logger.Printf("(= %T", update) w.worker.Logger.Printf("(= %T", update)
checkBounds := func(idx, size int) bool {
if idx < 0 || idx >= size {
return false
}
return true
}
switch update := update.(type) { switch update := update.(type) {
case *client.MailboxUpdate: case *client.MailboxUpdate:
status := update.Mailbox status := update.Mailbox
@ -301,6 +307,10 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
case *client.MessageUpdate: case *client.MessageUpdate:
msg := update.Message msg := update.Message
if msg.Uid == 0 { if msg.Uid == 0 {
if ok := checkBounds(int(msg.SeqNum)-1, len(w.seqMap)); !ok {
w.worker.Logger.Println("MessageUpdate error: index out of range")
return
}
msg.Uid = w.seqMap[msg.SeqNum-1] msg.Uid = w.seqMap[msg.SeqNum-1]
} }
w.worker.PostMessage(&types.MessageInfo{ w.worker.PostMessage(&types.MessageInfo{
@ -314,6 +324,10 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
}, nil) }, nil)
case *client.ExpungeUpdate: case *client.ExpungeUpdate:
i := update.SeqNum - 1 i := update.SeqNum - 1
if ok := checkBounds(int(i), len(w.seqMap)); !ok {
w.worker.Logger.Println("ExpungeUpdate error: index out of range")
return
}
uid := w.seqMap[i] uid := w.seqMap[i]
w.seqMap = append(w.seqMap[:i], w.seqMap[i+1:]...) w.seqMap = append(w.seqMap[:i], w.seqMap[i+1:]...)
w.worker.PostMessage(&types.MessagesDeleted{ w.worker.PostMessage(&types.MessagesDeleted{